however "-eq [1-5]) " is the issue. I get the following error:
“You must provide a value expression on the right-hand side of the ‘-eq’ operator.”
how do I get $roo to check against 1, 2, 3, 4, and 5. And I do know I could use -or to do this but my real situation includes much more than 5 conditions.
Well, I’m assuming you mean “against 1, 2, 3, 4, OR 5.” It would be impossible for something to be equal to both 1 and 2.
It depends a bit on what $roo is supposed to contain. If $roo will contain an integer, then:
if ($roo -in @(1,2,3,4,5)) {}
Would check to see if $roo contained one of those integers. If $roo contains a string consisting only of “1”,“2”, etc. then:
if ($roo -in @(‘1’,‘2’,‘3’,‘4’,‘5’)) {}
However, is $roo contained something like “5.0” or “1.1” then there’s no easy way to provide a match set like you’re trying to do. PowerShell doesn’t implicitly match against array elements like some other languages (and, in any event, your original error was that you were using square brackets incorrectly). You’d could do:
The above program works great in my environment but I’m going to be adding many more computers and its not ideal to continue adding -or(s). So I tried this.
$roomnumber = $env:computername.subString(0,$env:computername.length-2)
if ($roomnumber -eq /^1[2-3][0-4]M) {
WScript.Network).AddWindowsPrinterConnection(‘\Printerserver\132M_C01’)
}
Also just for reference a $roomnumber will look something like “132M”.
Saddly this didnt work for me earlier.
$roo = 3
if ($roo -in @(‘1’,‘2’,‘3’,‘4’,‘5’)) {
write-output “WORKS!”
}
I got a lot of missing statement blocks after the condition.
The -eq operator means EXACTLY EQUAL TO. The way you’re using it is correct in some instances, but incorrect in others - it doesn’t process regular expressions, for example. You’d need to use -match in that case, for your $roomnumber comparison.
Additionally, PowerShell typically would put the regex into quotation marks, rather than using slashes as the delimiter, although .NET makes some allowances in that area.
What might make it easier to help you is if you just pull out one comparison at a time that you’re having a problem with, and let us hash through that one. Looking at your script, aside from your use of -eq rather than -match, I’m not sure where you’re actually having a problem.
If you intend to use this method for setting multiple printers for multiple locations, this might scale better. Especially if your room sets are non-sequential.