Problem stopping websites on remote servers

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Christopher Warren Christopher Warren 7 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35701
    Profile photo of Christopher Warren
    Christopher Warren
    Participant

    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 }}

    #35703
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    Here is a good example of how to pass the variable to the script block so the remote computer can use it.
    from http://www.powershellmagazine.com/2013/01/23/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"
    }
    
    #35709
    Profile photo of Christopher Warren
    Christopher Warren
    Participant

    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 }
    }

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.