0
votes

Is Invoke-Command not pulling my variables?

Im caught on this one. I could use an xtra set of eyes!

Im pulling services from a remote machine and assigning them by number then passing a stop / start to the remote machine based off user input. Im getting a argument on my variable.

Please forgive the code setup Im new and I write then I clean. Some services and names have been removed for privacy.

Code::

$prepend = "ssssssssss"
$append = "sss"
$Fprepend = "tttttttt"
$Fappend = "tt"
$sitenumber = Read-Host 'What is the site number? ex. 1111'
 $name = $prepend + $sitenumber + $append  
 $Fname = $Fname = $Fprepend + $sitenumber + $Fappend

      $global:i=0
Get-service -Name Service,Instance,Server,Integration,Data,Message,FTP,Provider -ComputerName $name |
Select @{Name="Item";Expression={$global:i++;$global:i}},Name -OutVariable menu | Format-Table -AutoSize

$r = Read-Host "Select a service to restart by number"
$svc = $menu | where {$_.item -eq $r}

Write-Host "Restarting $($svc.name)" -ForegroundColor Green

Invoke-Command -ComputerName $Fname -ScriptBlock {Stop-Service -Name $svc.name -Force}
 sleep 3
Invoke-Command -ComputerName $Fname -ScriptBlock {Start-Service -Name $svc.name -Force}
Get-service -Name $svc.name -Computername $name

Error::

Cannot bind argument to parameter 'Name' because it is null. + CategoryInfo : InvalidData: (:) [Stop-Service], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StopServiceCommand

Cannot bind argument to parameter 'Name' because it is null. + CategoryInfo : InvalidData: (:) [Start-Service], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StartServiceCommand

1
No, Invoke-Command does not remote variables (the dynamic nature of scripts means it would be really hard to know which ones to remote in general) so there is no variable $svc on the remote end when the scriptblock is passed. From 3.0 onwards, you can fix this with $using. See about_Remote_Variables. - Jeroen Mostert
Thanks Jeroen, I passed a -ArgumentList $Service at the end of the Invoke to call my variable. I also wanted to capture the $svc.name into a seperate variable for later use. Now it looks like this per line $svc = $menu | where {$_item -eq $r} $Service = $svc.name Invoke-Command -ComputerName $Fname -ScriptBlock {Start-Service -Name $args[0]} -ArgumentList $Service - DotSlashCrash

1 Answers

0
votes

I have modified the code and now its working fine. The issue what you are facing is because inside the script block , $svc is not holding any value because its not in the scope even. To make it within the scope you have to pass as an ArgumentList and it has to be initiated inside the block as param. Thats why ou are getting as Null

Use the below code. I just modified the Invoke part

$prepend = "ssssssssss"
$append = "sss"
$Fprepend = "tttttttt"
$Fappend = "tt"
$sitenumber = Read-Host 'What is the site number? ex. 1111'
 $name = $prepend + $sitenumber + $append  
 $Fname = $Fname = $Fprepend + $sitenumber + $Fappend

      $global:i=0
Get-service -Name Service,Instance,Server,Integration,Data,Message,FTP,Provider -ComputerName $name |
Select @{Name="Item";Expression={$global:i++;$global:i}},Name -OutVariable menu | Format-Table -AutoSize

$r = Read-Host "Select a service to restart by number"
$svc = $menu | where {$_.item -eq $r}

Write-Host "Restarting $($svc.name)" -ForegroundColor Green
# You have to pass the $svc as an argumentlist and the same has to be initiated as param inside the script block.
Invoke-Command -ComputerName $Fname -ScriptBlock {param($svc)Stop-Service -Name $svc.name -Force} -ArgumentList $svc
 sleep 3
Invoke-Command -ComputerName $Fname -ScriptBlock {param($svc)Start-Service -Name $svc.name -Force} -ArgumentList $svc
Get-service -Name $svc.name -Computername $name

Hope you understand the issue now.