Checking presence or folders remotely

Welcome Forums General PowerShell Q&A Checking presence or folders remotely

This topic contains 2 replies, has 2 voices, and was last updated by

4 years, 7 months ago.

  • Author
  • #18060

    Points: 0
    Rank: Member

    Hi, I am doing a migration from FRS to DFS replication of SYSVOL folder. In one step I want to check if SYSVOL and SYSVOL_DFSR folders both exist in Windows folder on all domain controllers. I did this in the PowerShell:

    Invoke-Command -ComputerName localhost,dc02,drdc,slt62000svad,slt62800svad -ScriptBlock {

    $computer = hostname
    $path = "c:\Windows"
    Write-Host "On computer $computer the following sysvol folders exist:"
    Get-ChildItem -Path $path | Where-Object {$_.PsIsContainer -and $PSItem.Name -like "*sysvol*"}
    Write-Host ""


    I got list of folders with name like sysvol only for localhost (dc with all fsmo roles), for others nothing has been retrieved. Why? I tried also this in script block but to no avail:

    Get-ChildItem -Path $env:SystemRoot | Where-Object {$_.PsIsContainer -and $PSItem.Name -like "*sysvol*"}

  • #18063

    Points: 240
    Helping Hand
    Rank: Participant

    Try this:

    $Result = Invoke-Command -ComputerName localhost,dc02,drdc,slt62000svad,slt62800svad -ScriptBlock {
        if (Get-ChildItem -Path $env:SystemRoot | Where-Object {$_.PsIsContainer -and $PSItem.Name -like "*sysvol*"}) {
            $Result="The folder 'SYSVOL*' exists on server $env:COMPUTERNAME"
        } else {
            $Result="The folder 'SYSVOL*' does NOT exist on server $env:COMPUTERNAME"
        return $Result

    You want to avoid write-host – remember to save the puppies (:D)
    You want to check if these computers exist/online/WinRM available via a try statement first, otherwise this will error out for each computer that cannot be reached.
    Most importantly, your script did not work because its output could not cross the Invoke command ScriptBlock context (scope) barrier.
    To get data back from a ScriptBlock use a variable assignment as above and "return" (or use the job cmdlets) – you can only return 1 variable
    To send variable data from main script to ScriptBlocks use -ArgumentList switch and Param() within the ScriptBlock..

  • #18064

    Points: 0
    Rank: Member

    I put Write-Host just to distinct server I retrieved data from – ps script was not in focus but migration itself. Any way good to refresh my memory about sending variable data. A dozen more lessons and I will cover entire Don's CBT PowerShell Ultimate Training – that DSC is soooo cool stuff. I am creating my ps book based on Don's training so it will be precious material since I like ps very much – but the year ago I hated it. And everything started last November when I had to disable 50 user accounts and decided to automate that. From then on I use PS every day (AD, Exchange, registry, file . . .). Awesome tool is System Engineer arsenal.

The topic ‘Checking presence or folders remotely’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort