Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Swink
Apr 18, 2006
Left Side <--- Many Whelps
So we're almost finished migrating to 2008R2 and Exchange 2010, and I heard on the wire that you can manipulate mailbox permissions using PS. For example, a user would call me and say that they need to give editor writes on their inbox to their new assistant.

Could I do up a script in PS that would do this for me? If so, is there anything I can't do with powershell & exchange?

Adbot
ADBOT LOVES YOU

Swink
Apr 18, 2006
Left Side <--- Many Whelps
I'm no exchange admin myself, but Mailbox/Folder permissions are a daily thing. So this all looks very promising. Thanks!

Swink
Apr 18, 2006
Left Side <--- Many Whelps
This will be an easy one. The following lists all subfolders of all our exchange mailboxes.


code:
get-mailbox | get-mailboxfolderstatistics | Select Name,FolderPath,FolderType
emsInFolder,FolderSize,Identity | Sort-Object FolderSize -descending | Export-Csv c:\folders.csv
When I export to CSV, the foldersize comes out as '97.04 MB (101,752,614 bytes)' Which excel cannot sort. Is it possible to spit that out as just MBs so I dont have to do another layer of fixing up in Excel?

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Thanks, that sorted me out.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
How can I validate user input? I have a script that creates AD users, and I need to specify the users' location. There are four options, how can I ensure I dont misspell the city when I'm typing it in? Is there any way I could choose from a list of options rather than having to type?

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Thanks guys. This is working perfectly.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
I need to set a HTML signature for a stack of OWA users. I grabbed this script from the web

code:
$mailboxes = get-mailbox 
$mailboxes| foreach {$file= "c:\signatures\" + ($_.samAccountName) + ".htm"; Set-MailboxMessageConfiguration -identity $_.samAccountNAme -SignatureHtml "$(Get-Content -Path $file -ReadCount 0)" -AutoAddSignature $True}
Which gets all the mailboxes, grabs the same-named html file from the folder and applies it to the OWA. What I want to do is turn it around so instead of iterating through every mailbox, it iterates through the files in c:\signatures and applies to the same-named mailbox.

The reason being I have 1000 mailboxes but only 500 OWA users, and I only know who they are based on whether they have a sig file in the folder.


Edit: Did it all by myself! Look at me go!

code:
$sigs = gci C:\sigs\ 

$sigs | foreach {$file = $_.FullName; Set-MailboxMessageConfiguration -identity $_.name -SignatureHtml "$(Get-Content -Path $File -ReadCount 0)" -AutoAddSignature $True}

Swink fucked around with this message at 07:03 on Sep 4, 2012

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Never mind!

Swink fucked around with this message at 12:33 on Sep 11, 2012

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Anyone else getting a shitload of yellow warning errors with 3.0? I'm getting them with every command. Although it might be the exchange2010 snapin I'm using.

Also, half of the commands dont have a help entry, and update-help doesnt work. Running as Admin or otherwise.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Am I using If statements correctly?

code:
$Location = "Melbourne"

If ($location = "Sydney") {
Do things
}

Elseif ($location = "Melbourne") {
Do nothing
}

When I run the above, when it hits the first If statement it changes the variable to "Sydney" and runs the code for the Sydney location. How should I be writing it?

EDIT - Solved. need to use -eq instead of the "=" sign.



New Question:

How easy is it to create a web frontend for powershell scripts? For instance a user creation script with a dropdown box to select department.

Swink fucked around with this message at 07:30 on Apr 16, 2013

Swink
Apr 18, 2006
Left Side <--- Many Whelps

evil_bunnY posted:

It would be trivial to make a CLI script user-friendly instead.

You're right. You're all right. I needed that reality check.

Is there a prettier method of getting the user to make a selection? At the moment I use this:


Switch ($Choice)
{
1 {$location = "Sydney"}
2 {$location = "Melbourne"}
3 {$location = "Brisbane"}
}

Which I find a bit hideous.

Swink
Apr 18, 2006
Left Side <--- Many Whelps

Red Robin Hood posted:

Has anyone gone through the CBT Nuggest Powershell videos? I just finished the first video and it seems like it is going to be a good learning experience but I wanted to get some more input.

This video from Don Jones taught me everything and its free - https://www.youtube.com/watch?v=-Ya1dQ1Igkc

Swink
Apr 18, 2006
Left Side <--- Many Whelps
I'm pulling a telephone number from AD and trying to convert it to a string, so I can use substring to get the last three numbers.


code:
$number = get-aduser -Identity [i]username[/i] -properties telephonenumber | select telephonenumber

$number = $number.tostring()
At this point the variable is empty. Why is that? What's the correct way to get that number as a string?


Edit - Solved it already.

$number = get-aduser -Identity username -properties telephonenumber | select -expandproperty telephonenumber

Swink fucked around with this message at 23:35 on Nov 26, 2013

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Couldn't give us a look at that script could you?

Swink
Apr 18, 2006
Left Side <--- Many Whelps
^ I hate seeing those in scripts. I never remember which is which.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Does a last run property actually exist?

I've always assumed you need 3rd party software to track that kind of thing.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
I need to open WINWORD.EXE, wait for it to open (it loads some templates from a network share), then close it.

Is there a more elegant way than using

code:
Start-Process Winword
start-sleep -x 
Stop-Process -Name Winword

Swink
Apr 18, 2006
Left Side <--- Many Whelps

Briantist posted:

You can use automation:

code:
$word = New-Object -Com Word.Application
$word.Visible = $false    # This is the default; change it to $true if you want to see the window.
$word.Quit()
In my test with Measure-Command, the time taken for this is 100 - 200ms but I don't have it loading templates, so I can't tell if it's waiting or not.




It doesnt load anything when launching this way. I need Word to run all its "stuff" as if it was launched by a user. I managed to get it to work by opening an actual word doc that contains an "on open" Macro to quit the Word Application. It works well enough.
code:
 $Word = New-Object -ComObject Word.application
 $word.visible = $false
 $Doc = $Word.Documents.Open("C:\template\CLOSE WORD.docm")

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Thats even better. Thanks.



New query: Can I create an empty hashtable, fill it with data and then splat it?

I'm reading data from a form, putting it in an array with the intention to use it for Set-ADUser @param


This is what I have so far, but i cant call @userDetails. How should I be setting this up?
code:
$UserDetails = @{}

$UserDetails.add("firstName",  $reader.AcroFields.GetField("FIRST"))
$UserDetails.add("middleName",   $reader.AcroFields.GetField("MIDDLE"))
$UserDetails.add("lastName",  $reader.AcroFields.GetField("SURNAME"))
$UserDetails.add("Position",  $reader.AcroFields.GetField("PositionTitle"))

set-aduser @userDetails

Edit. I think I answered my own question by reading the error message: "The splatting operator '@' cannot be used to reference variables in an expression. '@UserDetails' can be used only as an argument to a command."

I have to change my hashtable keys to the same name as the command I"m going to use them on. There's no -FirstName command for set-mailbox.

Swink fucked around with this message at 04:07 on Dec 11, 2014

Swink
Apr 18, 2006
Left Side <--- Many Whelps
I'm replacing vbs login scripts with powershell versions. Whats the proper way to handle execution policy on workstations? Is it a bad idea to set them all as unrestricted?

Should I sign each of my scripts? And do they need to be resigned each time they're modified?

Swink fucked around with this message at 02:04 on Jan 12, 2015

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Some will, but some will just be called by the user via double clicking a batch script, or in the startup directory.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Perfect. You are a golden goose!

Swink
Apr 18, 2006
Left Side <--- Many Whelps
What is the go with powershell's ability to manage network gear? Could I use it to enable/disable switch ports to a schedule, or is that not how it works?

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Briantist: I just realised you're briantist.com. A blog I've referenced several times.

Nice shout out from Ned Pyle the other day.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
That din jones video taught me as well. I recommend him to everyone.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
I've done something stupid and run a start-dscconfiguration on my workstation. Now it processes the config every 30 mins which includes renaming my PC and a heap of other poo poo I do not want.

How do I tell the LCM to stop processing the DSC config?

Swink
Apr 18, 2006
Left Side <--- Many Whelps
That seems to only stop an in-progress configuration.

I got around it by generating a new config with no configurations and applied it.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Unrelated but does that win32_product weirdness also apply when running 'wmic product' from the command line?

Swink
Apr 18, 2006
Left Side <--- Many Whelps
For the record, I have not seen any crazy reconfiguration of all packages when using wmic product. Nope Wmic product totally reconfigs every goddamn package which explains why it takes so long to return results.

I'm specifically using it for broken packages* that wont remove correctly so maybe it should still be avoided in normal scenarious.

New Q:

What is a simple option for source control? I want it to be free and private. Can I set up Git on Windows and sync it with Dropbox? I'd use GitHub for Windows except I dont want to pay for a private repository at this stage.



*Java

Swink fucked around with this message at 12:21 on Jul 15, 2015

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Right, cool. I was unfamiliar with the relationship with git, github and other services like bitbucket.

I'm familiarish with command line Git so I'm happy I can just use that. Private repos on BitBucket is handy.


hihifellow posted:

So I was using this for software uninstalls, and after all the posts here I went looking for alternatives and found the regkey HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall which lists everything installed on the system by UID along with the uninstall string, which if it's an MSI is just "MSIEXEC /X{UID}". Was easy to script and faster to boot, since everything I'm working with is installed via MSI anyway.

This is the way I usually enumerate and unininstall programs.

Swink fucked around with this message at 12:06 on Jul 15, 2015

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Double post but I wanted to seperate this out.

I'm trying to focus on abstraction and parametising the stuff I write so I can be a ~good coder~. I'm having trouble figuring out when and why its necessary on some scripts.

Take this script I wrote to warn users of an impending password expiry. It's going to run every day or so as a scheduled task. How could I improve it? What could I gain by improving it? Is this an example of a script that doesnt really need to be anything more than it is?



code:
$alerttime = (get-date).adddays(10)

$alertusers = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} -SearchBase "OU=users,DC=domain,DC=com" `
 –Properties "samaccountname","DisplayName","emailAddress", "msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property "samaccountname","Displayname","emailAddress",@{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} | where {$_.expirydate -le $alerttime }  

 foreach ($user in $alertusers){
        
         $emailBody = "<html>

	        <body>
		        <p>
			        <span style='font-family:arial,helvetica,sans-serif;'>Hi " + $user.Displayname + ",</p>
		      Your password will expire at  " + $user.ExpiryDate  + ". You must change your password soon.
	        </body>
        </html>"

 Send-MailMessage -From [email]Isupport@company.com[/email] -Subject "Password Expiry Reminder" -To $user.emailAddress -Body $emailBody -BodyAsHtml -SmtpServer smtp.server.com
 }

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Good responses, thanks all.


Ithaqua posted:

Can't you just configure the group policy that notifies users literally every time they log in that their AD password is going to expire in [X] days?

The answer to this was already put forward, but I've had the CEO overseas with only his iPad. He got locked out and had to wait until he was back in local business hours to get support. The email is specifically for that scenario. Plus stacks of users just dont heed the popup.


Briantist, you just demonstrated supreme aptitude. The HTML template has applications to some other god-awful scripts I have in place. For me and my current org I don't see the value of parametrizing that particular script in this particular scenario. You've left me with a great example for the future though.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
For exchange you need to add-pssnapin <name of the exchange snapin>

I have no idea why it's different to say, AD, which is a module that can auto-load.

I'll grab the exact snappin name in when I'm back at my desk. It'll be different depending which version you're running. I run 2010 so YMMV for other versions.



Edit: For Exchange 2010 SP3:

code:
 $exchange = New-PSSession <servername>
Enter-PSSession $exchange
Add-PSSnapin  Microsoft.Exchange.Management.PowerShell.E2010
For the record, it never actually works from my workstation. I get this error:
code:
 get-mailbox
Value cannot be null.
Parameter name: serverSettings
    + CategoryInfo          : NotSpecified: (:) [Get-Mailbox], ArgumentNullException
    + FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.Exchange.Management.RecipientTasks.GetMailbox

Unsure if this is my broken client\server or the broken module.

Swink fucked around with this message at 01:37 on Jul 21, 2015

Swink
Apr 18, 2006
Left Side <--- Many Whelps
^ Yeah or just do that. :)

Swink
Apr 18, 2006
Left Side <--- Many Whelps

Tony Montana posted:

What are the major features of 5 that I should be aware of?

SSH? So you can do encrypted sessions? What else?

The goddamn package manager!

code:
PS C:\Users\9thg> find-package *notepad*

Name                           Version          Source           Summary
----                           -------          ------           -------
Devbox-Notepad2                4.2.25           chocolatey       A fast and extremly light-weight Notepad-like text ...
notepadplusplus-withuninstall  6.6.2            chocolatey       Notepad++ is a free (as in "free speech" and also a...
notepadplusplus                6.7.9.2          chocolatey       Notepad++ is a free (as in "free speech" and also a...
notepadplusplus.install        6.7.9.2          chocolatey       Notepad++ is a free (as in "free speech" and also a...
notepadplusplus.commandline    6.7.9.2          chocolatey       Notepad++ is a free (as in "free speech" and also a...
Notepadplusplus.Settings       1.0.0.20141029   chocolatey       Allows Notepad++ settings to be installed from a pr...
notepad2                       4.2.25.3         chocolatey       A fast and light-weight Notepad-like text editor wi...
notepad2-mod                   4.2.25.940       chocolatey       A modified version (fork) of Notepad2 based on Kai ...
notepadreplacer                1.1.6            chocolatey       Replace notepad.exe with your favorite editor instead.
ProgrammersNotepad             2.3              chocolatey       Programmers Notepad - Windows programming editor wi...
XmlNotepad                     2007.0.0.0       chocolatey       XmlNotepad




install-package notepad2


Swink
Apr 18, 2006
Left Side <--- Many Whelps
I've wasted hours on that same thing.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
I hate that % alias.

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Is anyone using the http://psappdeploytoolkit.com and is it a worthwhile thing to use in a SME?

edit - for context, we install\update software via GPO however with more and more mobile staff, less people are actually booting up in the office so I need a method to push out software when the machine is online.

Swink fucked around with this message at 08:24 on Sep 15, 2015

Swink
Apr 18, 2006
Left Side <--- Many Whelps

Doug posted:

I can't comment specifically on that, but we use PDQ Deploy and it's been really great. [/url]

I'm all about PDQ :) the beauty of the powershell toolkit (for me) is the notifications for the users. Allows them to defer etc.

Might be question for the windows thread.

Adbot
ADBOT LOVES YOU

Swink
Apr 18, 2006
Left Side <--- Many Whelps
Double post for this: https://twitter.com/Johnmroos/status/643915357384740865


PowershellGet Support in PS 3.0 and 4.0

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply