我看到一些与我的类似问题的问题,但是我认为 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
这确实有意义,因为它试图在本地机器上找到一个不存在的集合。
我的问题是:
最佳答案
对于那些对完整解决方案感到好奇的人。请看下文。
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
关于powershell - 如何在远程服务器上成功调用命令到 Add-WebConfigurationProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44102690/