 |
|
| IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012PowerShellCommunity.org is moving! This community software, and the hardware that it sits on, are no longer serving the purposes of this community. As a result, we have decided to move this community to a new home at PowerShell.org. PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site. We've already started getting some great questions from members of the community over there so please, come on over and join us!
While we are going through this transition, this site will remain up for the short term. New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.
[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.
If you have any questions, please let us know on the PowerShell.org site.
Thank you,
Kirk "Poshoholic" Munro |
|
|
|
|
Use output for new command
Last Post 12 Apr 2012 06:25 AM by EBGreen. 5 Replies.
|
Sort:
|
|
Prev Next |
You are not authorized to post a reply. |
|
ceem
 New Member Posts:15

 |
| 10 Apr 2012 11:44 PM |
|
Hey folks, I'm still not very used to powershell so I have some questions: I want to use the Output of my command for another command: (Get-((get-ExchangeServer (get-content env:computername)).Serverrole)Server) The Output of Get-ExchangeServer "hostname".Serverrole is either Mailbox or Transport
So Basicly I want to determine the Serverrole of an Exchangeserver and get the Information out of it. Either its Get-Transportserver or Get-Mailboxserver Obviously my commund up there doesnt work, does anyone know how to do this? Should I create a new alias? Also the hostname doesnt seem to get recognized like that what type of quotes should i use?
Thanks
P.S. Also I have troubles understanding all those regular expressions, does anyone have a link to a good ressource understanding them?
|
|
|
|
|
EBGreen
 Veteran Member Posts:1276

 |
| 11 Apr 2012 05:10 AM |
|
I think that you are trying to do too much in one line. It is a difficult temptation to resist in Powershell sometimes. See if something like this works better: $serverType = Get-ExchangeServer $env:ComputerName | Select-Object -expand ServerRole if($serverType -eq 'Mailbox'){ Get-MailBoxServer $env:ComputerName }else{ Get-TransportServer $env:ComputerName That is off the cuff so it probably isn't exactly right, but it should give you the idea. |
|
| "Look Ma...no strings!" |
|
|
ceem
 New Member Posts:15

 |
| 11 Apr 2012 05:59 AM |
|
Thanks for the answer. I haven't tried it out yet but it seems like i have to use variable whenever I want to do something fancy with an object?
for example: $lastmonth = (get-date).AddMonths(-1) | Get-date -format MM
if ($lastmonth -eq '01'){
}
why can't I just use the above command directly in the if statement since I know I wont use the variable more than one time? And how do I put it directly into the if statement without an error? Am I so dependant on variables?
|
|
|
|
|
EBGreen
 Veteran Member Posts:1276

 |
| 11 Apr 2012 06:11 AM |
|
You don't always have to use a variable. Personally I tend to because it makes code easier to maintain in my opinion, but you can certainly do this: if((get-Date).AddMonths(-1).ToString('MM') -eq '01'){ |
|
| "Look Ma...no strings!" |
|
|
ceem
 New Member Posts:15

 |
| 12 Apr 2012 12:49 AM |
|
Thanks for your help, I obviously didnt know the Method ToString was able to output it into a certain format. I have another question concerning the dates. I got 12 directories in a folder with the monthly names (January, February ... December) Now how do Select them all using the names? (They are all part of the Date object somehow since the output of the Date is 'MMMM' Of course I could do it the way everyone would do it (select them all by the creation date) but if there were other folders in that directory i don't want to select I had a problem. So I want to select January - December using the date object, how would you do that?
edit: ok I got to some point:
For ($i=0; $i -lt 12; $i++) { (get-date).AddMonths(+$i).ToString('MMMM') } The problem now is that it starts at April but I want it to start from January. I made up a code but isn't there a "cleaner" solution? For ($i=1; $i -lt 13; $i++) { $jan = (get-date).ToString('MM') (get-date).AddMonths(+$i-$jan).ToString('MMMM') }
|
|
|
|
|
EBGreen
 Veteran Member Posts:1276

 |
| 12 Apr 2012 06:25 AM |
|
As in any good scripting language, there are multiple ways to accomplish any task. In this case personally, I think that I would do it like this: $monthList = @('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December') Then loop through that. A more dynamic example that is technically more portable since it would provide proper localization would be: $monthList = (0..11) | %{(Get-Date "01/01/01").AddMonths($_).ToString("MMMM")} Same array, but as I say if you ran it in a different locale it would provide proper month names. |
|
| "Look Ma...no strings!" |
|
|
| You are not authorized to post a reply. |
|
Active Forums 4.3
|
|
 |