bbi*_*ose 2 iis powershell function web
我正在使用新的自给自足的PowerShell脚本更新和替换旧的过时的每晚安排的批处理文件.
我遇到麻烦的是网站启动.我能够使用当前的测试代码来获取站点列表并显示它们但是如果它们被停止则无法找到正确启动它们的方法.
Set-ExecutionPolicy Unrestricted
start-transcript -path C:\TESTSTART.TXT
#function to get list of websites that are currently stopped
function Get-StoppedSites {
Import-Module WebAdministration
$website = Get-ChildItem IIS:\Sites | where {$_.State -eq 'Stopped'}
write-host "$website is stopped"
}
#function to start the stopped website
function StartSites {
param(
$websitename
)
start-website $websitename
write-host "$website was started"
}
$Script = Get-StoppedSites
$Script | ForEach-Object{StartSites -websitename $website}
stop-transcript
Run Code Online (Sandbox Code Playgroud)
当我从powershell调用它时,它的行为就像它完成没有错误,但网站没有启动,我已经停止.Windows 2008 R2服务器,PS 2.0.一旦它准备推出,我将在2003服务器上运行它.
这是我的成绩单:
Transcript started, output file is C:\TESTSTART.TXT
is stopped
Start-Website : Cannot validate argument on parameter 'Name'. The argument is null. Supply a non-null argument and try
the command again.
At C:\Users\Administrator\Desktop\test.ps1:14 char:18
+ start-website <<<< $websitename
+ CategoryInfo : InvalidData: (:) [Start-Website], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.IIs.PowerShell.Provider.StartWebsiteCommand
was started
**********************
Windows PowerShell Transcript End
End time: 20120823150248
Run Code Online (Sandbox Code Playgroud)
你能帮助我弄清楚我在哪里/我做错了吗?谢谢!
试试这个:
Set-ExecutionPolicy Unrestricted
Import-Module WebAdministration
$sites = Get-Website | where {$_.State -eq 'Stopped'}
$sites | ForEach-Object {
$sitename = $_.name
IF ($sitename -ne $NULL)
{
Write-Host "$sitename is stopped. Starting it..."
Start-Website $sitename
}
}
Run Code Online (Sandbox Code Playgroud)
您的代码存在一些问题.最重要的Get-StoppedSites
是,实际上并没有返回任何东西,只是打印出名字.此外,您可以使用现有的命令行开关Get-Website
来获取网站.