Unpacking in Powershell

Hello everyone,

I could use a bit of help on a script I am writing. I normally code in Python and it has the ability to unpack data into multiple variables. For example (a, b, c) = (1, 2, 3) where a =1, b=2,c=3 and can now be manipulated independently. Is there a similar ability in Powershell?

Here is a sample of my script. I import a csv file that has three columns of data with the following headings(labels) Box, Username, Password. I want to unpack each column into its on variable so that I can plug them in to the Invoke-VMscript cmdlet.

$file = Import-Csv user_pass.csv

foreach ($file.Box, $file.Username, $file.Password in $file){

    $vm = $file.Box

    $username = $file.username

    $Password = $file.password

    Invoke-VMScript -vm $vm -ScriptText {systeminfo.exe | Select-String -pattern 'Os Name','Os Version'} -GuestUser $username -GuestPassword $password | out-file $vm

    Invoke-VMScript -vm $vm -ScriptText {wmic product}  -GuestUser $username -GuestPassword $password | out-file -Append $vm

}

I have been banging my head on this for a few days now any help would be greatly appreciated.

Hello again,

Well I solved my own problem so I thought I would share my solution in case someone has the same issue. I realized that when I import the file in to my $file variable that it is an object that has the properties of the three column labels in my csv, Box, Username, Password. This allowed me to reference them inside of my loop by unpacking/ assigning each label in $line into a specific variable.

$file = Import-Csv user_pass.csv


foreach ($line in $file){
    $box = $line.Box 

    $username = $line.Username

    $Password = $line.Password

    Invoke-VMScript -vm $box -ScriptText {systeminfo.exe | Select-String -pattern 'Os Name','Os Version'} -GuestUser $username -GuestPassword $password | out-file $box
    Invoke-VMScript -vm $box -ScriptText {wmic product}  -GuestUser $username -GuestPassword $password | out-file -Append $box


I Hope this helps someone else. Also if any of the pros out there have any wisdom to share it is totally welcome. Please excuse any incorrect references to Powershell nomenclature.

Fletcher,
Welcome to the forum. :wave:t4:

Actually you don’t need to define additional variables. You can use them directly.

$file = Import-Csv -Path user_pass.csv
foreach ($line in $file) {
    Invoke-VMScript -vm $line.Box -GuestUser $line.Username -GuestPassword $line.Password -ScriptText { systeminfo.exe | Select-String -Pattern 'Os Name', 'Os Version' } | Out-File -FilePath $line.Box
    Invoke-VMScript -vm $line.Box -GuestUser $line.Username -GuestPassword $line.Password -ScriptText { wmic product } | Out-File -FilePath $line.Box -Append
}

To make your code a little bit easier to read you can splatting:

$file = Import-Csv -Path user_pass.csv
foreach ($line in $file) {
    $InvokeVMScriptParams = @{
        vm            = $line.Box
        ScriptText    = { systeminfo.exe | Select-String -Pattern 'Os Name', 'Os Version' }
        GuestUser     = $line.Username
        GuestPassword = $line.Password
    }
    Invoke-VMScript @InvokeVMScriptParams | Out-File -FilePath $line.Box

    $InvokeVMScriptParams = @{
        vm            = $line.Box
        ScriptText    = { wmic product }
        GuestUser     = $line.Username
        GuestPassword = $line.Password
    }
    Invoke-VMScript @InvokeVMScriptParams | Out-File -FilePath $line.Box -Append
}
1 Like

Awesome! Thanks for the welcome and Powershell wisdom!!