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
skipdogg
Nov 29, 2004
Resident SRT-4 Expert

I'll be following this like a hawk. I need to learn powershell badly, but everytime I bust out the book to try to figure something out I just revert back to a batch file to do it. I have a hell of a time with any programming type syntax, just makes no sense to me.

Adbot
ADBOT LOVES YOU

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

adaz posted:

Next time you have a problem that you want to use a batch/vbscript file for post it here, can show you how it's done in powershell.

Well I actually cheat and use a program called Batch-O-Matic most of the time.

Here's a situation I would be interested in figuring out, and you'll probably laugh at how simple it is, but like I mentioned, I suck at this.

I have a roaming profile server, there are about 700 directories in there. I need to delete a folder in each one of them for a program we no longer use, it would free up some space and reduce the size of my backup set a ton. Each folder is about 80 megs.

I have a text file with the name of each folder on a separate line, but I guess my disconnect is how to get the command to loop and substitute the name in the next line of the file.

So what I need to do is delete a folder 'Spark' inside of each users directory.

\\roamserver\profiles\%username%\ntprofile\Spark


Normally I would use the old batch o matic, but I need to learn to do this the right way and not rely on questionable programs off the internets.

So I know the command to delete the folder would be
code:
Remove-Item \\roamserver\profiles\%username%\ntprofile\Spark -recurse
But how do I get the list of 700 variables for %username% to feed into it, and repeat until done? Maybe with a log file output saying \\roamserver\profiles\%username%\ntprofile\Spark deleted successfully? That would be icing on the cake.

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

Thanks guys! I'll give this a whirl and see how it works. I really would like to automate terminating employees in AD somewhat ( I work in a high turnover environment ) but I want to try to give it a go before relying on you fine folks.

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

Adaz and co.

No idea if you still check this thread, but if you could help me out I would appreciate it.

A task I would like to accomplish is this. I have 300 Windows XP computers and I need to change the MTU setting on the ethernet adapter to a value of 1300. I'm guessing powershell can easily help me do this.

I suck at programming. I know what steps I need to accomplish, but stringing them together just blows my mind.

I have a .csv file of the computer names available.

I know I need to do the following, but translating it to code is just :psyboom: for me. To give you an idea of what I need to accomplish

1: I need to import a computer name to perform additional actions on
2: I need to read the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards key on the computer in step 1. There's a sub entry with a random number, then under that there is a string value called 'ServiceName' with a value of some random id.

example
code:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\8]
"ServiceName"="{76373BA6-7201-4780-BEC2-206EFE6F3617}"
"Description"="Intel(R) 82567LM-3 Gigabit Network Connection"
3: I need to somehow single out the value of "ServiceName" into a variable and then have the script connect to this key

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\%step2variable%

and after it connects to that key, I need it to write a new value under that key of "MTU" with data of '1300'


A less elegant but just as effective way would be just just apply the MTU of 1300 to every adapter under HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\ as the machines only have 1 adapter anyway. Not sure how I could do that either.

Any pointers or suggestions to get my going in the right direction would be super helpful.

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

That is way more elegant than what I was going to suggest. I was going to suggest getting a list of all users by doing a get-aduser and pulling their sAMAccountName then pumping that into the foreach statement.

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

kiwid posted:

How does one get a list of proxyAddresses from AD with the users name with each proxyAddress in a separate row?

For example:

pre:
|  name  |     proxyAddress    |
--------------------------------
|  user1 |  email1@domain.com  |
|  user1 |  email2@domain.com  |
|  user1 |  email3@domain.com  |
|  user2 |  email1@domain.com  |
|  user3 |  email1@domain.com  |
|  user1 |  email2@domain.com  |
|  user4 |  email1@domain.com  |

We're an O365 shop as well, so we've done a lot with powershell and AD/Exchange.

For this issue, I basically stole the solution from this blog. Multivalue properties can be a little bit of a pain. My buddy at work is our 'powershell master' and he even wrote something that just pulls the @onmicrosoft.com proxy address from a user object.

http://blogs.technet.com/b/heyscriptingguy/archive/2013/07/22/export-user-names-and-proxy-addresses-to-csv-file.aspx

To comment on one of your previous posts

kiwid posted:

So, we've recently changed our policy for our help desk to start filling in all the fields in an Active Directory user that we never really cared about before, such as organization information, addresses, phone numbers, etc. We've also moved to Office 365 which requires manually editing the proxyAddresses attribute for alias email addresses. They aren't very consistent with filling this information out and often forget to connect home drives and poo poo. I want to create a dummy proof PowerShell script that goes through the process of creating a new user but my experience with PowerShell is running one off commands. If you guys had to choose one book (maybe two), to learn PowerShell (especially syntax), what would it be?

Powershell in a month of lunches is really good. I would start there.

We have some user onboard scripts written in powershell that completely automate the new user creation process, but they're written for bulk new users in our call center environment where 20,40,60 people have mostly the same attributes.

If you can organize the data into a CSV file though, it's pretty easy to automate. You could also just write a script that prompts for each bit of data, that might work better.

Helpdesk dude runs new_user.ps1 and then it prompts for

First Name
Last Name
Logon Name
Email Address
<whatever else you want>
etc.

Then you can throw in some logging, it's really just a matter of what you want to do with powershell.

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

I'm sure it can be done in powershell, but have you looked at the File Server Resource Management tool? You can setup quite a few alerts to be emailed in there. I haven't looked recently, but might want to check that out before spending time on making powershell do it.

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

Microsoft is doing a couple of live events later this month covering DSC if anyone is interested. These tend to be recorded and can be watched at a later date as well

http://www.microsoftvirtualacademy.com/liveevents/getting-started-with-powershell-desired-state-configuration-dsc

http://www.microsoftvirtualacademy.com/liveevents/advanced-powershell-desired-state-configuration-dsc-and-custom-resources

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

Briantist posted:


If you've finished the book, I think you should just start using PowerShell for yourself. As you go along you'll have questions and problems and then you can figure those out or post them.

Agreed. I didn't start getting better with powershell until I started using it for tasks at work.

Oh hey, I need to get the amount of RAM installed on this list of 30 servers, how can I use Powershell to get that information.

Over the last year I've built up a decent collection of starter scripts and one liners that I can modify for whatever I need. I mostly manage AD and users so most of my scripts are for those type of tasks.

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

Avenging_Mikon posted:

Just ordered it. I probably won't get to powershell much, but this is the first excuse I've had to use it at all!

It's a good book, but I learned powershell by just googling tasks I wanted to do and built on things from there.

Also, I wouldn't directly modify the userAccountControl values manually, change the appropriate settings using the proper command (see anthonypants post about set-aduser and set-adaccountcontrol). I don't have a good reason to say not to do this, just personal preference.

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

Powershell can do a lot, but I'd start looking at different tools.

Check out Selenium for the web browser stuff

http://www.seleniumhq.org/

skipdogg
Nov 29, 2004
Resident SRT-4 Expert


Not sure if you saw, but Snover is going to be at a Powershell User Group in NYC in June.

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

I would have done it like PBS posted, I'm not very good at PS or elegant, but I did something similar a while back and this is what I did. I usually use import-csv for actual CSVs with headers, and get-content with txt files with single entries per line, but I'm guessing that CSV is single entry per line as well.

code:
$computers = get-content C:\support\testnames.csv
$array= @()

foreach ($computer in $computers){

$pathresult = Test-Path -Path "HKLM:\Software\Microsoft\RPC\Internet"

$results = New-Object -TypeName PSObject -Property @{
"Hostname" = $computer
"Path Exists" = $pathresult
}
$array += $results

}
$array | Export-CSV c:\support\Pathresults.csv -NoTypeInformation

It should pop out a CSV report for you. The columns on my test report are backwards, and I don't know how to fix that, but that's an easy fix in excel. I'm sure there are 50 different better ways to do this, but it should work.

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

I know you're not the AD admin, but something is funky if you're getting bounced around to all sorts of DC's and a couple of DC's are unresponsive.

If everything is setup properly you should only be dealing with DC's in your local site, or the assigned DC for your site.

I wouldn't worry about hard coding to a single server. It takes a ton to bog down a DC. I have ones that handle thousands of logins a minute and something like 300 ldap queries a second and they don't even blink at the load. It's not ideal though, I personally don't like it when folks hardcode to a certain server as it causes issues during DC upgrades and replacements... but we also keep the environment updated properly so we don't run into issues you're seeing.

Adbot
ADBOT LOVES YOU

skipdogg
Nov 29, 2004
Resident SRT-4 Expert

Is there a way for powershell to wait for a command to finish in a for each loop before moving on to the next one? Google has lead me to start-process possibly.

More info:

I'm migrating a bunch of file shares and I have to run an .exe based utility against each one. Currently I'm doing this manually and it sucks. If I put the .exe in a for each loop though I think it's going to launch the .exe X number of times, which on some file servers could be 200+ shares. To add a wrinkle I need to modify the argument list of the .exe for every iteration

this isn't exact code, but loosely an idea of what I'm thinking about

code:
$shares = get-content c:\sharename.txt

foreach ($share in $shares){

c:\migration\$share\utility.exe /arg1 /arg2 /arg3     

Now I'm pretty sure if I run that it'll spawn utility.exe for however many $shares there are. What I need is for it to just run one at a time and when one finishes it launches another. In a perfect world I'd like to be able to run 4-6 iterations of utility.exe at a time, but I won't get greedy.

If someone could point me in the right direction I'd appreciate it.

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