Checking presence or folders remotely

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Bojan Zivkovic Bojan Zivkovic 2 years, 3 months ago.

  • Author
    Posts
  • #18060
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic
    Participant

    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
    Profile photo of Sam Boutros
    Sam Boutros
    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
    }
    $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
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic
    Participant

    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.

You must be logged in to reply to this topic.