我看到几个关于类似于我的问题的问题,但是我认为WebAdministration模块cmdlet可能在它们中有些含糊不清。
我正在尝试做什么: 在远程服务器上执行以下操作:
Remove-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$FBFolderName/User/$Username" -filter "system.webServer/webdav/authoringRules" -name "."
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$FBFolderName/User/$Username" -filter "system.webServer/webdav/authoringRules" -name "." -value @{users="$DomainUser";access='Read,Write,Source';path='*'}
当我在服务器上运行它们时,这些工作完全正常,当我以交互方式运行它们创建New-PSSession时,它也能正常工作
但是,当我以非交互方式运行以下脚本时,我收到的错误似乎表明这些指令在本地运行,而不是在有问题的远程服务器上运行:
$FBFolderName = "FB Demo"
$Username = "user"
$AllUsersRole = "DOMAIN\GTM All Users"
$DomainUser= 'DOMAIN\' + $Username
$s = New-PSSession -computerName server.domain.local
$finalcommand = Remove-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$FBFolderName/User/$Username" -filter "system.webServer/webdav/authoringRules" -name "."
Invoke-Command -Session $s -ScriptBlock { Import-module WebAdministration; $finalcommand }
$finalcommand = Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$FBFolderName/User/$Username" -filter "system.webServer/webdav/authoringRules" -name "." -value @{users="$DomainUser" access='Read,Write,Source';path='*'}
Invoke-Command -Session $s -ScriptBlock { $finalcommand }
Remove-PSSession $s
以非交互方式运行时,收到以下错误:
Remove-WebConfigurationProperty : Filename:
Error: Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/FB Demo'
At line:1 char:17
+ $finalcommand = Remove-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Remove-WebConfigurationProperty], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.IIs.PowerShell.Provider.RemoveConfigurationPropertyCommand
这确实有意义,因为它试图在本地机器上找到一个不存在的集合。
我的问题是:
答案 0 :(得分:1)
对于那些对完整解决方案充满好奇的人。请看下面。
Add-WebConfigurationProperty和Add-WebConfigurationProerty的语法非常繁琐。提示:确保不要无意中添加" /"在MACHINE / WEBROOT / APPHOST结束时。它会让你发疯,误导误导。
$ServerFQDN = "server.domain.local"
$FBFolderName = "FolderName" # This is the name of your IIS Site
$Username = "User"
$DomainUser = "DOMAIN\User"
$s = New-PSSession -computerName $ServerFQDN
$commandScriptBlock = {
Import-Module WebAdministration
Write-Host "Setting WebDAV Permissions ..."
Try {
Remove-WebConfigurationProperty -ErrorAction SilentlyContinue -pspath "MACHINE/WEBROOT/APPHOST" -Location "$($args[0])/User/$($args[1])" -Filter "system.webServer/webdav/authoringRules" -name "."
Write-Host -NoNewline " Removing All Users from Authoring Rules ..."
Write-Host -ForegroundColor Green " [ OK ]"
}
Catch {
Write-Host -NoNewline " Removing All Users from Authoring Rules ..."
Write-Host -ForegroundColor Red " [ ERROR ] "
Write-Output $($_.Exception.Message)
}
Try {
Add-WebConfigurationProperty -ErrorAction SilentlyContinue -pspath "MACHINE/WEBROOT/APPHOST" -Location "$($args[0])/User/$($args[1])" -Filter "system.webServer/webdav/authoringRules" -name "." -value @{users="$($args[2])";access='Read,Write,Source';path='*'}
Write-Host -NoNewline " Adding user $($args[2]) to Authoring Rules ..."
Write-Host -ForegroundColor Green " [ OK ]"
}
catch {
Write-Host -NoNewline " Adding user $($args[2]) to Authoring Rules ..."
Write-Host -ForegroundColor red " [ ERROR ] "
Write-Output $($_.Exception.Message)
}
}
Invoke-Command -Session $s -ScriptBlock $commandScriptBlock -ArgumentList $FBFolderName,$Username,$DomainUser
Remove-PSSession $s
答案 1 :(得分:0)
您正在将命令存储在本地变量中,该变量在远程会话中是未知的:
$finalcommand = Remove-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$FBFolderName/User/$Username" -filter "system.webServer/webdav/authoringRules" -name "."
将所有命令存储在scriptblock中:
$commandScriptBlock= {
Import-module WebAdministration
Remove-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location "$FBFolderName/User/$Username" -filter "system.webServer/webdav/authoringRules" -name "."
}
通过以下方式调用scriptblock:
Invoke-Command -Session $s -ScriptBlock $commandScriptBlock
作为替代方案,您还可以将$finalcommand
作为参数传递给Invoke-Command
cmdlet。有关如何执行此操作的详细信息,请参阅此link。