header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

if statement within a cmdlet
Last Post 24 Sep 2008 02:58 PM by Shay. 6 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
24 Sep 2008 06:03 AM  

I'm stuck a bit on a script I am working on and hopefully this is possible.

 

I am doing a get-wmiobject  and one of the things I am doing is any time an Edge server is checked, I do a get-credential since it's a workgroup machine.  Because of that, I am wanting to do an if ($server.isEdgeServer -eq $true) { -Credential get-Credential }.

 

But I can't figure out how to do something like get-wmiobject Computer $server -class classhere ifstatementhere.  This way if it's not an Edge Server the command will run without the ifstatement conflicting with the get-wmiobjectcommand but if it's an Edge server, the ifstatement will run but it'll use -Credential Get-Credential.

 

Is there a way to do what I am looking for?

Elan Shudnow
http://www.shudnow.net
ShayUser is Online
Basic Member
Basic Member
Posts:281
Avatar

--
24 Sep 2008 01:18 PM  

What command produce the isEdgeServer member?

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
24 Sep 2008 01:47 PM  

Get-WmiObject Win32_LogicalDisk -namespace root\cimv2 –filter "DriveType = 3" -ComputerName $server @(if ($server.isEdgeServer -eq "True") { Get-Credential })

That's what I have in my script right now.  If the server is an Edge Server, they'll get prompted successfully for authentication.  Once the user authenticates, the entire if statement gets passed backed into the command and the user gets access denied.  Because of this, if the server is not an Edge server, the if statement gets fed right into the command right off the bat which means it fails right away for non-Edge servers.

Elan Shudnow
http://www.shudnow.net
ShayUser is Online
Basic Member
Basic Member
Posts:281
Avatar

--
24 Sep 2008 02:24 PM  
Try:

if ($server.isEdgeServer){
Get-WmiObject Win32_LogicalDisk –filter "DriveType=3" -ComputerName $server -Credential (Get-Credential)
} else {
Get-WmiObject Win32_LogicalDisk –filter "DriveType=3" -ComputerName $server
}
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
24 Sep 2008 02:28 PM  

Ya, I was thinking about that, but the reason why I was asked if I can do it the way I was trying is because I am creating a bunch of functions in my sript that will be doing this.  The code I posted was just the first line of the function of code.  Because of this, if I do it with an if Edge else, I will have to manage two different long code sets which would I wanted to avoid.

Elan Shudnow
http://www.shudnow.net
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
24 Sep 2008 02:43 PM  

Got it working, thanks again Shay.

I did:

$(if ($server.isEdgeServer) { Get-WmiObject Win32_LogicalDisk -namespace root\cimv2 –filter "DriveType = 3" -ComputerName $server -Credential (Get-Credential) }) | Format-Table etc...

If I did just the if, I found out the pipeline doesn't accept input from a statement, only commands.  Because of this, I had to make the if statement appear as a command doing the $(if).

Elan Shudnow
http://www.shudnow.net
ShayUser is Online
Basic Member
Basic Member
Posts:281
Avatar

--
24 Sep 2008 02:58 PM  
Move the closing if "}" to the end of the format-table call



Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer