0

I am a total novice when it comes to powershell. I would like to create a little script to save me a lot of time and after a little research, I am sure it can be achieved using Import-CSV command.

Basically I need to run a command on multiple PC's but the variable is different for each command. I wish to pull that variable from a comparision in a CSV file. So find current Hostname, then use that hostname to find the corresponding asset number in the CSV and then use that asset number as a variable in the final comamnd.

Looking at other examples on here, I have this so far:

$Asset = @()
$Host = @()

Import-Csv -Path "C:\hostnametoasset.csv" |`
ForEach-Object {
    $Asset += $_.Asset
    $Host += $_.Host
}

$Hostname = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name

if ($Host -contains $Hostname)
{
C:\BiosConfigUtility64.exe /setvalue:"Asset Tracking Number","$Asset"
}

Section of the CSV:

Asset,Host
10756,PCD001
10324,PCD002
10620,PCD003

Any help would be greatly appreciated.

2
  • 2
    1. Don't use $Host as a variable name (it's a built-in variable). 2. You're not using any host name in the BIOS utility configuration command, so it's not clear why you need a host name in the first place. Commented Aug 8, 2018 at 20:06
  • yes I swapped $Host to $Name and updated my CSV. The host name is needed for comparision in the CSV to return the asset number Commented Aug 9, 2018 at 11:44

1 Answer 1

3

Couple of different points... Importing a CSV results in an array of objects that you can filter on.

$Lines = Import-Csv -Path "C:\hostnametoasset.csv"
$Line = $Lines | ?{$_.host -match $ENV:COMPUTERNAME}

You can then use the filter results directly by accessing the member you need.

C:\BiosConfigUtility64.exe /setvalue:"Asset Tracking Number","$($Line.Asset)"

NOTE: I cannot test this directly right now so hopefully I got the syntax right.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks very much. the final script looks like this: $csv = Import-Csv -Path "\\lsqus009\HPSSM\BCU\hostnametoasset.csv" $Line = $csv | ?{$_.Name -match $ENV:COMPUTERNAME} \\lsqus009\HPSSM\BCU\BiosConfigUtility64.exe /setvalue:"Asset Tracking Number","$($Line.Asset)" and it works beautifully

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.