Hi,
I hope someone can help me with this. I’m new to powershell and I’m trying to create a script that will go through my list of servers and stop a particular website from running.
When I run my script the error I get is “Cannot validate argument on parameter ‘Name’. The argument is null. Provide a valid value for the argument, and then try running the command again.”
I have a feeling this is because I need to pass an argument on to the scriptblock but I have been unable to figure it out.
$Servers = Get-content “c:\scripts\servers.txt”
#$site = Get-Content C:\Users\admincw\Desktop\STOPsitename.txt
$site = Read-Host -Prompt ‘Input Site Name’
ForEach ($server in $Servers) { Invoke-Command $server -ScriptBlock {
Import-Module webadministration
Stop-Website -Name $site
Get-WebsiteState -Name $site }}
Here is a good example of how to pass the variable to the script block so the remote computer can use it.
from #PSTip Passing local variables to a remote session in PowerShell 3.0
for powershell 2.0
$a = "PowerShell"
$b = "Rocks"
Invoke-Command -ComputerName Server01 -ScriptBlock {
param ($first,$second)
Write-Output "The value of $a is: $first"
Write-Output "The value of $b is: $second"
} -ArgumentList $a,$b
For powershell 3.0 and above
$a = "PowerShell"
$b = "Rocks"
Invoke-Command -ComputerName Server01 -ScriptBlock {
Write-Output "The value of
$a is: $using:a"
Write-Output "The value of `$b is: $using:b"
}
That worked!! Thank you Jonathan!
$Servers = Get-content “c:\scripts\servers.txt”
#$site = Get-Content C:\Users\admincw\Desktop\STOPsitename.txt
$site = Read-Host -Prompt ‘Input Site Name’
ForEach ($server in $Servers) { Invoke-Command $server -ScriptBlock {
Import-Module webadministration
Stop-Website -Name $using:site
Get-WebsiteState -Name $using:site }
}