@rebootssolveproblems<\/a><\/p>","upvoteCount":1,"datePublished":"2017-08-17T14:54:59.000Z","url":"https://community.spiceworks.com/t/compare-a-variable-to-a-string/600540/5","author":{"@type":"Person","name":"edwineekelaers2","url":"https://community.spiceworks.com/u/edwineekelaers2"}},{"@type":"Answer","text":"Yeah just something to keep in mind using powershell, one of the many gotchas that you’ll forget once or twice<\/p>\n
C is similar, if x=4 sets x equal to 4, while x==4 compares x and 4. And its not like it errors out, or gives warnings like “Hey I see you have an if statement but then you are assigning a value to a variable, are you sure you’ve had enough coffee today??”<\/p>","upvoteCount":2,"datePublished":"2017-08-17T15:13:16.000Z","url":"https://community.spiceworks.com/t/compare-a-variable-to-a-string/600540/6","author":{"@type":"Person","name":"matthart5","url":"https://community.spiceworks.com/u/matthart5"}},{"@type":"Answer","text":"\n\n
<\/div>\n
CrazyLefty:<\/div>\n
\nare you sure you’ve had enough coffee today?<\/p>\n<\/blockquote>\n<\/aside>\n
<\/p>","upvoteCount":0,"datePublished":"2017-08-17T15:16:01.000Z","url":"https://community.spiceworks.com/t/compare-a-variable-to-a-string/600540/7","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Neally:<\/div>\n
\n\n\n
<\/div>\n
CrazyLefty:<\/div>\n
\nare you sure you’ve had enough coffee today?<\/p>\n<\/blockquote>\n<\/aside>\n<\/blockquote>\n<\/aside>\n
Ha! I printed that .gif out and hung it in my cubicle. I can say I’m off to a good start since I can see the printout jittering!<\/p>","upvoteCount":0,"datePublished":"2017-08-17T15:19:12.000Z","url":"https://community.spiceworks.com/t/compare-a-variable-to-a-string/600540/8","author":{"@type":"Person","name":"matthart5","url":"https://community.spiceworks.com/u/matthart5"}}]}}
My script is setting my variable rather than testing to it against a string:
$Model = (Get-WmiObject -Class:Win32_ComputerSystem).Model
If ($Model = "Latitude E6430")
{
Set-Location -Path \\contosofs\software$\"Dell Drivers"\"DELL LatE6430"\BIOS\
Copy-Item -Path E6430A21.exe -Destination C:\Temp
Set-Location C:\Temp
.\E6430A21.exe
}
elseif ($Model = "Latitude E6420")
{
Set-Location -Path \\contosofs\software$\"Dell Drivers"\"DELL LatE6420"\BIOS\
Copy-Item -Path Latitude_E6420_E6420ATG_A24.exe -Destination C:\Temp
Set-Location C:\Temp
.\Latitude_E6420_E6420ATG_A24.exe
}
else {
Write-Host "Does not match known Bios Config Vulnerability"
}
Can someone help me so that it tries to match it?
3 Spice ups
Neally
(Neally)
August 17, 2017, 2:47pm
2
You use ‘-eq’
= in powershell is ‘value’ assignment
# Assign
$model = 'Latitude E6430'
# Compare
$model -eq 'something' # will give true/false result
# so you'd use:
If ($Model -eq "Latitude E6430"){
# do stuff
}
https://ss64.com/ps/syntax-compare.html
5 Spice ups
Evan7191
(Evan7191)
August 17, 2017, 2:48pm
3
Have you tried using -eq instead of = in your If statements?
2 Spice ups
dancrane
(dancrane)
August 17, 2017, 2:50pm
4
Neally:
You use ‘-eq’
I laughed because I’ve done exactly this, not once, but twice!
1 Spice up
$model=(get-wmiobject win32_computersystem).model
switch ($model) {
"Latitude E6430" {"Yay, it's the one"; break}
default {"Oh no"; break}
}
Something like that and then between the { } you put the commands you want executed… To add more selections just copy the line starting with “Lattitude …”, paste it and change the selection criteria and then what has to be executed.
Info here
And why not start-process your way around this…
Set-Location C:\Temp
.\E6430A21.exe
You can even steal another line of code from it and do it like this
switch ($(get-wmiobject win32_computersystem).model) {
"Your model" {"Yay, it's the one"; break}
default {"Oh no"; break}
}
@rebootssolveproblems
1 Spice up
matthart5
(CrazyLefty)
August 17, 2017, 3:13pm
6
Yeah just something to keep in mind using powershell, one of the many gotchas that you’ll forget once or twice
C is similar, if x=4 sets x equal to 4, while x==4 compares x and 4. And its not like it errors out, or gives warnings like “Hey I see you have an if statement but then you are assigning a value to a variable, are you sure you’ve had enough coffee today??”
2 Spice ups
Neally
(Neally)
August 17, 2017, 3:16pm
7
matthart5
(CrazyLefty)
August 17, 2017, 3:19pm
8
Neally:
Ha! I printed that .gif out and hung it in my cubicle. I can say I’m off to a good start since I can see the printout jittering!