ondrovic
ondrovic

Reputation: 1175

Powershell PSSession Not Working

Trying to test PSSession with powershell

Enter-PSSession -ComputerName <servername>
Invoke-Command -ComputerName <servername> -ScripBlock {
Get-PSDrive -PSProvider FileSystem | %{ write-host 'Checking drive ' $_.Root; set-location $_.Root; Get-ChildItem -Filter "load4.wav" -Recurse; write-host 'Checked drive ' $_.Root; } | Out-File "$env:UserProfile\Desktop\$env:ComputerName-Results.txt"
}

I would eventually like to be able to run this against a list of servers and save save the results locally if possible

So when I run it will enter the PSSession

[server]: PS C:\Users\US_Chris.Ondrovic\Documents>

But will not execute this part of the code

Invoke-Command -ComputerName server -ScripBlock {
Get-PSDrive -PSProvider FileSystem | %{ write-host 'Checking drive ' $_.Root; set-location $_.Root; Get-ChildItem -Filter "load4.wav" -Recurse; write-host 'Checked drive ' $_.Root; } | Out-File "$env:UserProfile\Desktop\$env:ComputerName-Results.txt"
}

So I can verify this works like it is supposed to on the remote machine

Invoke-Command -ComputerName <server> -ScriptBlock {Get-Service} | Out-File $env:Userprofile\Desktop\Test.txt

Got it some what working, now it runs on through the PSSession but isn't looking for what I need

function Remote-Search() {
param([string[]]$Servers, [string]$Item)

foreach($server in $Servers) {
    Invoke-Command -ComputerName $server -ScriptBlock {Get-PSDrive -PSProvider FileSystem | %{ write-host 'Checking drive ' $_.Root; set-location $_.Root; Get-ChildItem -Filter $item -Recurse; write-host 'Checked drive ' $_.Root; }} | Out-File $env:Userprofile\Desktop\$server-results-$item.txt
    }
}

Upvotes: 1

Views: 450

Answers (2)

briantist
briantist

Reputation: 47792

function Remote-Search() {
    param([string[]]$Servers, [string]$Item)

    foreach($server in $Servers) {
        Invoke-Command -ComputerName $server -ArgumentList $Item -ScriptBlock { param([string]$filter)
            Get-PSDrive -PSProvider FileSystem | %{ 
                write-host 'Checking drive ' $_.Root
                set-location $_.Root
                Get-ChildItem -Filter $filter -Recurse
                write-host 'Checked drive ' $_.Root 
            }
        } | Out-File $env:Userprofile\Desktop\$server-results-$item.txt
    } 
}

Variables in your script aren't available directly to the script block. You can declare parameters in the script block and then pass values to them through the -ArgumentList parameter of Invoke-Command.

So here, I've added a $filter parameter to the script block, and I passed in $Item through the -ArgumentList parameter of Invoke-Command. Now in the script block, on the remote machine, $filter will have that value.

Upvotes: 1

SomeShinyObject
SomeShinyObject

Reputation: 7801

Invoke-Command -ComputerName server -ScripBlock {
    Get-PSDrive -PSProvider FileSystem | %{ write-host 'Checking drive ' $_.Root; set-location $_.Root; Get-ChildItem -Filter "load4.wav" -Recurse; write-host 'Checked drive ' $_.Root; } | Out-File "$env:UserProfile\Desktop\$env:ComputerName-Results.txt"
}

Assuming this was copied verbatim, you are missing a "t" in -ScriptBlock

Upvotes: 0

Related Questions