函数最终应该这样做并运行以下命令:
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site' -filter 'system.web/customErrors' -name 'mode'
但是,变量像这样搞砸了,添加了当前工作目录并添加了反斜杠:
Get-WebConfigurationProperty : Cannot find path 'C:Usersusername'MACHINEWEBROOTAPPHOSTDefault Web Site''
because it does not exist
ECHO看起来不错,重定向到文件也很好:
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site' -filter 'system.web/customErrors' -name 'mode'
示例运行:
PS C:temp> Get-Check-Func2 "webcA" "mode" "system.web/customErrors"
'MACHINE/WEBROOT/APPHOST/Default Web Site'
system.web/customErrors
mode
Get-WebConfigurationProperty : Cannot find path 'C:Usersusername'MACHINEWEBROOTAPPHOSTDefault Web Site''
because it does not exist.
At line:10 char:16
+ ... Get-WebConfigurationProperty -pspath ` $mypspath ` -filte ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:Usersusern...fault Web Site':String) [Get-WebConfigurationProperty
], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.IIs.PowerShell.Provider.GetConfigurationPropertyCommand
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site' -filter 'system.web/customErrors' -name 'mode'
请善待,因为我是PowerShell的新手。
来自科恩/巴什背景。
function Get-Check-Func2 {
$a = Get-Website
$myws = $a.Name
$mypspath = 'MACHINE/WEBROOT/APPHOST/' + "$myws"
$mypspath = '''' + $mypspath + ''''
$myfilter = '''' + $args[2] + ''''
$myname = '''' + $args[1] + ''''
echo "$mypspath"
echo $args[2]
Get-WebConfigurationProperty -pspath ` $mypspath ` -filter ` $myfilter ` -name ` $myname
echo "Get-WebConfigurationProperty -pspath ` $mypspath ` -filter ` $myfilter ` -name ` $myname"
}
期望Get-WebConfigurationProperty运行,但似乎变量被搞砸了。
您的反引号可能会导致问题。取而代之的是:
Get-WebConfigurationProperty -pspath ` $mypspath ` -filter ` $myfilter ` -name ` $myname
只需这样做:
Get-WebConfigurationProperty -pspath $mypspath -filter $myfilter -name $myname
或者,如果您尝试多行它,请仅在每个参数/输入对之后放置反引号,例如:
Get-WebConfigurationProperty -pspath $mypspath `
-filter $myfilter `
-name $myname
向 cmdlet 提供大量参数(最终不会以很长的单行结束(的另一个常用选项是使用称为 splatting 的概念。例如:
$GetWebConfigPropertyParams = @{
PSPath = $mypspath
Filter = $myfilter
Name = $myname
}
Get-WebConfigProperty @GetWebConfigPropertyParams
看来你确实有正确的想法马克...我道歉。还从另一位Powershell大师那里得到了一些意见:-(现在工作正常!
function Check-Webca-Select {
param(
$WebsitePSPath = 'MACHINE/WEBROOT/APPHOST/',
$Filter ,
$ComponentName,
$Myexpect
)
$a = get-website
$w = $a.name
foreach ($WebsiteName in $w) {
$WebsiteFullPath = "{0}{1}" -f $WebsitePSPath,$WebsiteName
Get-WebConfigurationProperty -PSPath "$WebsiteFullPath" -Filter $Filter -Name $ComponentName | Select-String "$Myexpect"
}
}
执行示例
Check-Webca-Select -filter "system.web/customErrors" -ComponentName "mode" -myexpect "Remoteonly">