Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Command & variable substitution

Reply
 
Old 01-09-2008   #1 (permalink)
Chris


 
 

Command & variable substitution

Hello I have the following problem while creating a script which
contains the following line:

Script line:
get-wmiobject -class $strWMIClassName

Error description:
At xxx 1:74 char:15
+ get-wmiobject <<<< -class $strWMIClassName

As you can see the var is not substituted. I have tried all the ways I
do know from unix shells and so on how to substitute line $($var), `
$var`, ${var}....

Has anyone of you an idea how to solve this problem?

Thanks

Chris

My System SpecsSystem Spec
Old 01-09-2008   #2 (permalink)
Shay Levi


 
 

Re: Command & variable substitution


It works fine on v1 & v2

$strWMIClassName = "win32_process"
get-wmiobject -class $strWMIClassName


You didn't include the $strWMIClassName assignment in your post, check that
it's not $null



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Hello I have the following problem while creating a script which
> contains the following line:
>
> Script line:
> get-wmiobject -class $strWMIClassName
> Error description:
> At xxx 1:74 char:15
> + get-wmiobject <<<< -class $strWMIClassName
> As you can see the var is not substituted. I have tried all the ways I
> do know from unix shells and so on how to substitute line $($var), `
> $var`, ${var}....
>
> Has anyone of you an idea how to solve this problem?
>
> Thanks
>
> Chris
>

My System SpecsSystem Spec
Old 01-09-2008   #3 (permalink)
Chris


 
 

Re: Command & variable substitution

On 9 Jan., 13:17, Shay Levi <n...@xxxxxx> wrote:
Quote:

> It works fine on v1 & v2
>
> $strWMIClassName = "win32_process"
> get-wmiobject -class $strWMIClassName
>
> You didn't include the $strWMIClassName assignment in your post, check that
> it's not $null
>
> -----
> Shay Levi
> $cript Fanatichttp://scriptolog.blogspot.com
> Hebrew weblog:http://blogs.microsoft.co.il/blogs/scriptfanatic
>
Quote:

> > Hello I have the following problem while creating a script which
> > contains the following line:
Yes I haven't post it, because i didnt wan to post the whole
script ;-)

I have verified that the var is not null. it contains as example the
value Win32_Fan...

here my whole script...


###
# Script options

#
$boolDebug = $true

#
$strConfigFile = ".\WMIList.config"

#
$strCommentSign = "#"

#
$strDelimSign = ";"

# End script options
###

$intConfigCounter = 0

# Clear the screen
cls

# Read the configuration and exclude the comments
$strConfig = cat $strConfigFile | where
{$_.StartsWith($strCommentSign) -eq $false -and $_.Length -gt 0}

# Iterate all config lines (entries)
while ($intConfigCounter -lt $strConfig.Length) {

# Get the description of the current WMI Class out of the
config
# For this need all signs after the delim sign are stored
$strWMIClassDescription =
$strConfig[$intConfigCounter].Substring($strConfig[$intConfigCounter].IndexOf($strDelimSign)
+1)

# DEBUG
echo $strWMIClassDescription

# Get the name of the current WMI Class out of the config
# For this need remove the description, caused by this just
the name will return
$strWMIClassName =
$strConfig[$intConfigCounter].Remove($strConfig[$intConfigCounter].IndexOf($strDelimSign)
+1, $strWMIClassDescription.Length)

# DEBUG
echo $strWMIClassName
get-wmiobject -class $strWMIClassName

# Raise counter
$intConfigCounter = $intConfigCounter + 1

} # end while iterate all config entries

The script access a config file which contains as example:

# Comment
Win32_Fan;Blubb
Win32_Share;Blubber


Quote:
Quote:

> > Script line:
> > get-wmiobject -class $strWMIClassName
> > Error description:
> > At xxx 1:74 char:15
> > + get-wmiobject <<<< -class $strWMIClassName
> > As you can see the var is not substituted. I have tried all the ways I
> > do know from unix shells and so on how to substitute line $($var), `
> > $var`, ${var}....
>
Quote:

> > Has anyone of you an idea how to solve this problem?
>
Quote:

> > Thanks
>
Quote:

> > Chris
My System SpecsSystem Spec
Old 01-09-2008   #4 (permalink)
Shay Levi


 
 

Re: Command & variable substitution

Does this work?

$strWMIClassName = "win32_process"
get-wmiobject -class $strWMIClassName



If so, check your script. What's the output of echo $strWMIClassDescription
each iteration?

I suggest to download PowerGUI. It has a built-in script debugger you can
use to find what's wrong.
You can download it at:

http://powergui.org/downloads.jspa



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> On 9 Jan., 13:17, Shay Levi <n...@xxxxxx> wrote:
>
Quote:

>> It works fine on v1 & v2
>>
>> $strWMIClassName = "win32_process"
>> get-wmiobject -class $strWMIClassName
>> You didn't include the $strWMIClassName assignment in your post,
>> check that it's not $null
>>
>> -----
>> Shay Levi
>> $cript Fanatichttp://scriptolog.blogspot.com
>> Hebrew weblog:http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> Hello I have the following problem while creating a script which
>>> contains the following line:
>>>
> Yes I haven't post it, because i didnt wan to post the whole script
> ;-)
>
> I have verified that the var is not null. it contains as example the
> value Win32_Fan...
>
> here my whole script...
>
> ###
> # Script options
> #
> $boolDebug = $true
> #
> $strConfigFile = ".\WMIList.config"
> #
> $strCommentSign = "#"
> #
> $strDelimSign = ";"
> # End script options
> ###
> $intConfigCounter = 0
>
> # Clear the screen
> cls
> # Read the configuration and exclude the comments
> $strConfig = cat $strConfigFile | where
> {$_.StartsWith($strCommentSign) -eq $false -and $_.Length -gt 0}
> # Iterate all config lines (entries)
> while ($intConfigCounter -lt $strConfig.Length) {
> # Get the description of the current WMI Class out of the
> config
> # For this need all signs after the delim sign are stored
> $strWMIClassDescription =
> $strConfig[$intConfigCounter].Substring($strConfig[$intConfigCounter].
> IndexOf($strDelimSign)
> +1)
>
> # DEBUG
> echo $strWMIClassDescription
> # Get the name of the current WMI Class out of the config
> # For this need remove the description, caused by this just
> the name will return
> $strWMIClassName =
> $strConfig[$intConfigCounter].Remove($strConfig[$intConfigCounter].Ind
> exOf($strDelimSign)
> +1, $strWMIClassDescription.Length)
>
> # DEBUG
> echo $strWMIClassName
> get-wmiobject -class $strWMIClassName
> # Raise counter
> $intConfigCounter = $intConfigCounter + 1
> } # end while iterate all config entries
>
> The script access a config file which contains as example:
>
> # Comment
> Win32_Fan;Blubb
> Win32_Share;Blubber
Quote:
Quote:

>>> Script line:
>>> get-wmiobject -class $strWMIClassName
>>> Error description:
>>> At xxx 1:74 char:15
>>> + get-wmiobject <<<< -class $strWMIClassName
>>> As you can see the var is not substituted. I have tried all the ways
>>> I
>>> do know from unix shells and so on how to substitute line $($var), `
>>> $var`, ${var}....
>>> Has anyone of you an idea how to solve this problem?
>>>
>>> Thanks
>>>
>>> Chris
>>>

My System SpecsSystem Spec
Old 01-09-2008   #5 (permalink)
Shay Levi


 
 

Re: Command & variable substitution

BTW, why all the description and comments stuff when all you need is to query
with WMI?
Woudn't it be much simpler to store your WMI class names in CSV file and
avoid the parsing?

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> On 9 Jan., 13:17, Shay Levi <n...@xxxxxx> wrote:
>
Quote:

>> It works fine on v1 & v2
>>
>> $strWMIClassName = "win32_process"
>> get-wmiobject -class $strWMIClassName
>> You didn't include the $strWMIClassName assignment in your post,
>> check that it's not $null
>>
>> -----
>> Shay Levi
>> $cript Fanatichttp://scriptolog.blogspot.com
>> Hebrew weblog:http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> Hello I have the following problem while creating a script which
>>> contains the following line:
>>>
> Yes I haven't post it, because i didnt wan to post the whole script
> ;-)
>
> I have verified that the var is not null. it contains as example the
> value Win32_Fan...
>
> here my whole script...
>
> ###
> # Script options
> #
> $boolDebug = $true
> #
> $strConfigFile = ".\WMIList.config"
> #
> $strCommentSign = "#"
> #
> $strDelimSign = ";"
> # End script options
> ###
> $intConfigCounter = 0
>
> # Clear the screen
> cls
> # Read the configuration and exclude the comments
> $strConfig = cat $strConfigFile | where
> {$_.StartsWith($strCommentSign) -eq $false -and $_.Length -gt 0}
> # Iterate all config lines (entries)
> while ($intConfigCounter -lt $strConfig.Length) {
> # Get the description of the current WMI Class out of the
> config
> # For this need all signs after the delim sign are stored
> $strWMIClassDescription =
> $strConfig[$intConfigCounter].Substring($strConfig[$intConfigCounter].
> IndexOf($strDelimSign)
> +1)
>
> # DEBUG
> echo $strWMIClassDescription
> # Get the name of the current WMI Class out of the config
> # For this need remove the description, caused by this just
> the name will return
> $strWMIClassName =
> $strConfig[$intConfigCounter].Remove($strConfig[$intConfigCounter].Ind
> exOf($strDelimSign)
> +1, $strWMIClassDescription.Length)
>
> # DEBUG
> echo $strWMIClassName
> get-wmiobject -class $strWMIClassName
> # Raise counter
> $intConfigCounter = $intConfigCounter + 1
> } # end while iterate all config entries
>
> The script access a config file which contains as example:
>
> # Comment
> Win32_Fan;Blubb
> Win32_Share;Blubber
Quote:
Quote:

>>> Script line:
>>> get-wmiobject -class $strWMIClassName
>>> Error description:
>>> At xxx 1:74 char:15
>>> + get-wmiobject <<<< -class $strWMIClassName
>>> As you can see the var is not substituted. I have tried all the ways
>>> I
>>> do know from unix shells and so on how to substitute line $($var), `
>>> $var`, ${var}....
>>> Has anyone of you an idea how to solve this problem?
>>>
>>> Thanks
>>>
>>> Chris
>>>

My System SpecsSystem Spec
Old 01-09-2008   #6 (permalink)
Shay Levi


 
 

Re: Command & variable substitution

Can you try this:

############ .\WMIList.config ###########

Win32_Fan;Blubb
#Win32_Share;Blubber
Win32_Share;Blubber
#Win32_Share;Blubber

############ .\WMIList.config ###########



cat C:\Scripts\temp\WMIList.config | where {$_.substring(0,1) -ne "#"} |
foreach {
$strWMIClassName = $_.substring(0,$_.indexOf(";"))
$strWMIClassName
#get-wmiobject -class $strWMIClassName
}


# output #
Win32_Fan
Win32_Share


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> On 9 Jan., 13:17, Shay Levi <n...@xxxxxx> wrote:
>
Quote:

>> It works fine on v1 & v2
>>
>> $strWMIClassName = "win32_process"
>> get-wmiobject -class $strWMIClassName
>> You didn't include the $strWMIClassName assignment in your post,
>> check that it's not $null
>>
>> -----
>> Shay Levi
>> $cript Fanatichttp://scriptolog.blogspot.com
>> Hebrew weblog:http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> Hello I have the following problem while creating a script which
>>> contains the following line:
>>>
> Yes I haven't post it, because i didnt wan to post the whole script
> ;-)
>
> I have verified that the var is not null. it contains as example the
> value Win32_Fan...
>
> here my whole script...
>
> ###
> # Script options
> #
> $boolDebug = $true
> #
> $strConfigFile = ".\WMIList.config"
> #
> $strCommentSign = "#"
> #
> $strDelimSign = ";"
> # End script options
> ###
> $intConfigCounter = 0
>
> # Clear the screen
> cls
> # Read the configuration and exclude the comments
> $strConfig = cat $strConfigFile | where
> {$_.StartsWith($strCommentSign) -eq $false -and $_.Length -gt 0}
> # Iterate all config lines (entries)
> while ($intConfigCounter -lt $strConfig.Length) {
> # Get the description of the current WMI Class out of the
> config
> # For this need all signs after the delim sign are stored
> $strWMIClassDescription =
> $strConfig[$intConfigCounter].Substring($strConfig[$intConfigCounter].
> IndexOf($strDelimSign)
> +1)
>
> # DEBUG
> echo $strWMIClassDescription
> # Get the name of the current WMI Class out of the config
> # For this need remove the description, caused by this just
> the name will return
> $strWMIClassName =
> $strConfig[$intConfigCounter].Remove($strConfig[$intConfigCounter].Ind
> exOf($strDelimSign)
> +1, $strWMIClassDescription.Length)
>
> # DEBUG
> echo $strWMIClassName
> get-wmiobject -class $strWMIClassName
> # Raise counter
> $intConfigCounter = $intConfigCounter + 1
> } # end while iterate all config entries
>
> The script access a config file which contains as example:
>
> # Comment
> Win32_Fan;Blubb
> Win32_Share;Blubber
Quote:
Quote:

>>> Script line:
>>> get-wmiobject -class $strWMIClassName
>>> Error description:
>>> At xxx 1:74 char:15
>>> + get-wmiobject <<<< -class $strWMIClassName
>>> As you can see the var is not substituted. I have tried all the ways
>>> I
>>> do know from unix shells and so on how to substitute line $($var), `
>>> $var`, ${var}....
>>> Has anyone of you an idea how to solve this problem?
>>>
>>> Thanks
>>>
>>> Chris
>>>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Command substitution PowerShell
How to perform variable substitution in powershell? PowerShell
Stumped by variable substitution PowerShell
RE: Execute from command line with environment variable in path PowerShell
Execute from command line with environment variable in path PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46