0

Using powershell i am stopping IIS web site

function GoOffLine([psobject] $obj)
{  
  Invoke-Command -ComputerName $obj.ComputerName -ScriptBlock {import-module WebAdministration; Stop-Website 'My-Website'}
  Start-Sleep -Seconds 3
  WriteMessage("Application is offline.")
}

The above script works fine as long as i have Web site name hard-coded in the script. However when i change the script and pass the web site name using variable i get error

function GoOffLine([psobject] $obj)
{  
  Invoke-Command -ComputerName $obj.ComputerName -ScriptBlock {import-module WebAdministration; Stop-Website $obj.WebsiteName}
  Start-Sleep -Seconds 3
  WriteMessage("Application is offline.")
}

Error

Cannot validate argument on parameter 'Name'. The argument is null. Provide a valid value for the argument, and then try running the command again.

LP13
  • 30,567
  • 53
  • 217
  • 400
  • What properties does `$obj` have (that you are passing to the function)? Also, your `WriteMessage` function call has incorrect syntax. It should be `WriteMessage "Application is offline."` (no parentheses). – Bill_Stewart Jan 23 '18 at 18:51
  • `WriteMessage` is private function which internally uses `Write-Host` – LP13 Jan 23 '18 at 19:09
  • 2
    Not the point. Your syntax is not correct. In PowerShell, we don't call functions using `( )` like in other languages. (It is valid syntactically, but wrong semantically.) – Bill_Stewart Jan 23 '18 at 19:12
  • @Bill_Stewart is right, and incidentally if you used `Set-StrictMode` it would call you out on that as well. With one argument you don't notice; with two you will be scratching your head why it doesn't work as expected. Break that habit ASAP. – briantist Jan 23 '18 at 20:44
  • 1
    @briantist the link provided helped. I fixed it with `Invoke-Command -ComputerName $obj.ComputerName -ScriptBlock { param($ws) import-module WebAdministration; Start-Website $ws} -ArgumentList $obj.WebsiteName` – LP13 Jan 23 '18 at 21:15

0 Answers0