canadapatrick
canadapatrick

Reputation: 11

Function not working even though variable looks fine

Function should end up doing like this and running the command:

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

However variable gets messed up like this with current working directory added and backslashes:

Get-WebConfigurationProperty : Cannot find path 'C:\Users\username\'MACHINE\WEBROOT\APPHOST\Default Web Site''
because it does not exist

ECHO looks fine and also redirect to file is fine as well:

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

Sample run:

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'

Please be kind as I am a newbie to PowerShell.

Coming from korn/bash background.

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"
     }

Expect Get-WebConfigurationProperty to run but seems like variable gets messed up.

Upvotes: 1

Views: 139

Answers (2)

canadapatrick
canadapatrick

Reputation: 11

Seems like you did have the right idea Mark ... my apologies. Also got some input from another powershell guru :-) This works fine now!

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 sample

Check-Webca-Select -Filter "system.web/customErrors" -ComponentName "mode" -Myexpect "Remoteonly"

Upvotes: 0

Mark Wragg
Mark Wragg

Reputation: 23415

Your backticks might be causing the issue. Instead of this:

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

Just do this:

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

Or if you're trying to multiline it, only put the backtick after each parameter / input pair, e.g:

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

Another popular option for providing lots of parameters to a cmdlet in a way that doesn't end up with a very long single line is to use a concept called splatting. For example:

$GetWebConfigPropertyParams = @{
    PSPath = $mypspath
    Filter = $myfilter
    Name = $myname
}
Get-WebConfigProperty @GetWebConfigPropertyParams

Upvotes: 2

Related Questions