即使变量看起来很好,功能也不起作用

时间:2019-05-28 16:00:50

标签: powershell

函数应该最终像这样并运行以下命令:

Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Default Web Site'  -filter  'system.web/customErrors'  -name  'mode'

但是,在添加了当前工作目录并加反斜杠的情况下,变量就这样混乱了:

Get-WebConfigurationProperty : Cannot find path 'C:\Users\username\'MACHINE\WEBROOT\APPHOST\Default 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:\Users\username\'MACHINE\WEBROOT\APPHOST\Default Web Site''
because it does not exist.
At line:10 char:16
+ ...             Get-WebConfigurationProperty -pspath ` $mypspath ` -filte ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\usern...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的新手。

来自korn / bash背景。

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运行,但似乎变量弄乱了。

2 个答案:

答案 0 :(得分:2)

您的反引号可能是导致此问题的原因。代替这个:

SELECT a.*, b.*
FROM ( SELECT *
       FROM a
       WHERE a.userID = 11
       LIMIT 10) as a
JOIN b
  ON a.orderID = b.orderID 

只需执行以下操作:

if (tabel$group == A) {
  newvar <- tabel$date1
} else if (tabel$group == B) {
  newvar <- tabel$date2
} else if (tabel$group == C) {
  newvar <- tabel$date3
} else if (tabel$group == D) {
  newvar <- tabel$date4
}

或者,如果您想对其进行多行处理,则只需在每个参数/输入对之后加反引号,例如:

Get-WebConfigurationProperty -pspath ` $mypspath ` -filter ` $myfilter ` -name ` $myname

另一个流行的选项是,为cmdlet提供很多参数,而不会以很长的一行结束。这是使用splatting的概念。例如:

Get-WebConfigurationProperty -pspath $mypspath -filter $myfilter -name $myname

答案 1 :(得分:0)

似乎您确实有正确的主意Mark ...我很抱歉。 还从另一个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"
 }
}

exec示例

Check-Webca-Select-筛选器“ system.web / customErrors” -ComponentName“ mode” -Myexpect“ Remoteonly”