locked
Powershell: Why don't these assignments match? RRS feed

  • Question

  • Okay, I admit it; I'm a total n00b. Take a look at this code scrap:

    $colItems = Get-WmiObject -Class "Win32_ComputerSystem" -Namespace "root\CIMV2"
    
    foreach ($objItem in $colItems) {
        Write-Host $objItem.Manufacturer
        [Environment]::SetEnvironmentVariable("Manufacturer", "$objItem.Manufacturer", "Machine")
        Write-Host $objItem.Model
        [Environment]::SetEnvironmentVariable("Model", "$objItem.Model", "Machine")
    }
    
    Write-Host $env:Manufacturer
    Write-Host $env:Model
    

    The results are: 

    Dell Inc.
    OptiPlex 7010

    (Double-backslash)CO-ITS-025407\root\CIMV2:Win32_ComputerSystem.Name="CO-ITS-025407".Manufacturer
    (Double-backslash)CO-ITS-025407\root\CIMV2:Win32_ComputerSystem.Name="CO-ITS-025407".Model

    In other words, attempting to assign the WMI object attribute value results in two different outcomes. I need the value, not the path to the value. What gives? :(

    Thanks.

    Thursday, March 6, 2014 7:39 PM

Answers

  • Don't use quotes around "$objItem.Manufacturer" and "$objItem.Model".

    Bill


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Mike H. Glenn Friday, March 7, 2014 1:07 PM
    Thursday, March 6, 2014 7:55 PM
  • When you display $env:Whatever , you're looking up a Process environment variable. Assuming that same variable doesn't exist as a User variable, it's initialized from the Machine variable's value when the process starts.

    When you change a Machine environment variable, it doesn't immediately update your Process variables (though you'll see the change take effect the next time a process starts.) That's what JRV was getting at when he mentioned "the following will not work until you restart PowerShell", and is most likely what was happening when you said "Host commands are still displaying the path."

    • Marked as answer by Mike H. Glenn Friday, March 7, 2014 1:08 PM
    Thursday, March 6, 2014 8:50 PM

All replies

  • Don't use quotes around "$objItem.Manufacturer" and "$objItem.Model".

    Bill


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Mike H. Glenn Friday, March 7, 2014 1:07 PM
    Thursday, March 6, 2014 7:55 PM
  • You cannot and should not copy VBScript or you will -get lost.

    This will work partly:

    $system=Get-WmiObject Win32_ComputerSystem
    Write-Host $system.Manufacturer
    [Environment]::SetEnvironmentVariable('Manufacturer', $system.Manufacturer, 'Machine')
    Write-Host $objItem.Model
    [Environment]::SetEnvironmentVariable('Model',$system.Model, 'Machine')
    
    # the following will not work until you restart PowerShell
    Write-Host $env:Manufacturer
    Write-Host $env:Model

    Try not putting quotes around everything that is not nailed down.


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 8:02 PM
  • This works

    $system=Get-WmiObject Win32_ComputerSystem
    Write-Host $system.Manufacturer
    [Environment]::SetEnvironmentVariable('Manufacturer', $system.Manufacturer, 'process')
    Write-Host $objItem.Model
    [Environment]::SetEnvironmentVariable('Model',$system.Model, 'process')
    
    Write-Host $env:Manufacturer
    Write-Host $env:Model


    ¯\_(ツ)_/¯

    Thursday, March 6, 2014 8:03 PM
  • You mean like this?

        [Environment]::SetEnvironmentVariable("Manufacturer", $objItem.Manufacturer, "Machine")
        [Environment]::SetEnvironmentVariable("Model", $objItem.Model, "Machine")

    Hm.... Okay; it's showing up correctly in the environment. Interestingly, the last two Write-Host commands are still displaying the path. Any way I can convince them to do otherwise?

    Again; thanks!


    • Edited by Mike H. Glenn Thursday, March 6, 2014 8:16 PM Clarification
    Thursday, March 6, 2014 8:14 PM
  • Why do you need to set them in the environment?

    Bill


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 6, 2014 8:23 PM
  • When you display $env:Whatever , you're looking up a Process environment variable. Assuming that same variable doesn't exist as a User variable, it's initialized from the Machine variable's value when the process starts.

    When you change a Machine environment variable, it doesn't immediately update your Process variables (though you'll see the change take effect the next time a process starts.) That's what JRV was getting at when he mentioned "the following will not work until you restart PowerShell", and is most likely what was happening when you said "Host commands are still displaying the path."

    • Marked as answer by Mike H. Glenn Friday, March 7, 2014 1:08 PM
    Thursday, March 6, 2014 8:50 PM
  • There are some third-party processes that need the info. Their own internal processes can take quite awhile to dig the same info up, so I'm looking into a way to jump-start things.

    Friday, March 7, 2014 1:07 PM
  • Ah; forgot about that. (I blame the head cold.)

    Thanks!

    Friday, March 7, 2014 1:08 PM