powershell - 如何在远程服务器上成功调用命令到 Add-WebConfigurationProperty

标签 powershell iis remote-server webdav

我看到一些与我的类似问题的问题,但是我认为 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

这确实有意义,因为它试图在本地机器上找到一个不存在的集合。

我的问题是:
  • 我做错了什么以允许它远程运行吗?
  • 在尝试开发此方法之前,我使用 psexec 和 appcmd 来做同样的事情,这很有效,但是 psexec 建立连接需要很长时间。
  • 我采取了错误的方法吗?有没有更好的方法在远程服务器上做同样的事情?
  • 最佳答案

    对于那些对完整解决方案感到好奇的人。请看下文。

    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/

    相关文章:

    powershell - 如何从另一个模块中包含的被调用函数调用 PowerShell 模块范围来访问变量?

    asp.net - IIS 重定向在没有尾部斜线的情况下无法工作

    mysql - 来自另一个远程 ip perl 的远程数据库连接

    python - 命令 "python setup.py egg_info"失败,错误代码为 1 in/tmp/pip-build-hn4hol_z/spacy/

    powershell - 在本地运行时,Invoke-Command是否在当前作用域或子作用域中运行?

    powershell - 在 Powershell 中运行 .cmd/.bat 脚本

    c# - 在 MVC3 中使用 Server.MapPath

    c# - 如何以编程方式启动 IIS 6.0 SMTP 虚拟服务器?

    tomcat - 如何在IntelliJ 14内置Tomcat服务器中配置关闭端口?

    powershell - Powershell-比较日期和CSV日期