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
Cronus
Mar 9, 2003

Hello beautiful.
This...is gonna get gross.
What are everyone's opinions on the Quest AD Cmdlets? Link

I know you can just use .NET to access directory services, but for some of the projects I've been building I find it amazing for user manipulation. I wrote a script recently to query AD and check password expiration dates, and then send custom emails based on certain thresholds for remote users who basically just check webmail, and output HTML reports (which management loves).
I've gone all in on Posh unless it really is just much easier doing a task with the standard CLI, but this is rare.

I got a script request that I'm pretty sure is uncharted territory and wondering if anyone has ideas. Google and MSDN have nothing.

Essentially one of our engineers lost a user's offline cache during a server migration this weekend, so they want me to make a script that will auto-extract data in said cache to a temp location just to play it safe. Now in WinXP this is easy: just use the CSCCMD utility. But in Vista/7, these tools simply don't work. The only other option seems to be using the WMI provider for Offline Files or call the API directly. I am by no means an expert coder but I have dabbled in PHP/Perl and SQL, this may be above my skill level.

I essentially want to know if I can enumerate the offline file listing and then build the code to extract said files from the CSC database to another folder, in case this is an issue down the line.

The WMI providers, etc can be found here. But reading through it just seems to show me how to list but not necessarily manipulate the files in there. Anyone got any ideas or breadcrumbs on where I should go with this?

Adbot
ADBOT LOVES YOU

Cronus
Mar 9, 2003

Hello beautiful.
This...is gonna get gross.
You're probably better off not doing it via the registry (which I find really cumbersome in PShell) and just use WMI.
code:
$data = gwmi -class Win32_NetworkAdapterConfiguration | where {$_.IPEnabled -match "True"}
This will get you the list of NICs that currently have an IP address and save the object into a variable, which is probably going to just be one result. For WinXP there is a SetMTU method you can apply, so...
code:
$data.SetMTU(1300)
If there are multiple results, you'll want to do a foreach-object loop to set the value.

That should do it. Documentation on the method is here in case you are curious. Seems like this method isn't supported in Windows 2003 but nothing about XP. I would definitely test the method on a VM first, the status codes are in that document too so you can see if it worked or not.

Also if you're ever curious about what methods are available to you, just pipe your object/command to:
code:
get-member -membertype method
and you'll get the goodies.

Hope this helps!

Cronus
Mar 9, 2003

Hello beautiful.
This...is gonna get gross.

Rabid Snake posted:

Is there an easy way to send an email to a email alias when a script throws an error? We're using Office 365 to handle our exchange server so the SMTPclient class won't work for us. We've tried to use outlook but it throws security errors. Anyone have experience with using outlook to send an email with a log attachment file when the script throws an error?

You should be able to use Send-MailMessage if you're using PS 2.0, and pass the $error variable to the body.

http://technet.microsoft.com/en-us/library/dd347693.aspx

I abuse the hell out of it for custom reporting, it's pretty flexible just make sure you process all your objects to formatted strings before attempting to send or it will just look like garbage.

Cronus
Mar 9, 2003

Hello beautiful.
This...is gonna get gross.
Pipe it to Select-Object for conversion to a CSV format:
code:
$var | select-object name, time, info | export-csv stuff.csv
When you do Get-EventLog it's returning an object, so to convert you have to pull data out of the object you want and Export to CSV/XML from there.

Format-List is really for display purposes or pure string output. So alternatively you can do this:

code:
$var | fl name, time, info | out-file stuff.txt
and you'll see the data you want, but in pure text. It just depends on what you want or what you plan to do with the results. I usually output to CSV so that I can collate them from multiple sources using Import-CSV.

Cronus
Mar 9, 2003

Hello beautiful.
This...is gonna get gross.
You'll laugh it's so easy, but there are a few gotchas:

code:
$updates = Get-ChildItem -Filter *.msu -Name

// the loop
$updates | ForEach-Object{Invoke-Expression -command "'fancy.exe $_ /flag1 /flag2'"}
There are a number of ways to approach it, this is just one way.

The key part here is the $_, which represents the current index within the array of items in $updates (the list of files, basically) that it iterates through.

Also, by default PS will not run commandline strings as is, so that's why you use Invoke-Expression to execute the commands. Note the single quotes inside the double, without them it will treat spaces as multiple lines which you don't want.

And how to do the whole thing in one line using aliases and no need for file outputs:
code:
gci *.msu -name | %{iex -command "'fancy.exe $_ /flag1 /flag2'"}

Cronus
Mar 9, 2003

Hello beautiful.
This...is gonna get gross.
I'm thinking this is due to how Powershell handles items by default based on their type. The type on that query is a ManagementObject, and not a System.Array type, which I think would give you the expected results.

I also tried it in 2008, but didn't get a count on a single return, I guess it just implies 1 when it doesn't return a result. That is pretty dumb.

So if you wanted to get a legitimate count, you could make a dummy variable and run the object through a quickie loop like so:

code:
$blah = gwmi -class Win32_LogicalDisk | where{$_.deviceid -match "C:"}

$dummycount = 0
$blah | %{$dummycount++}

echo "The true count of items is [$dummycount]"
It's like the SystemManagement objects aren't inheriting count/length properties, and yet when returning multiple items, it clearly works. But testing that goes way beyond my knowledge of the language, I just write stuff!

Adbot
ADBOT LOVES YOU

Cronus
Mar 9, 2003

Hello beautiful.
This...is gonna get gross.
Version 2 also has the very easy to work with Send-MailMessage built in, and if you have .NET 4.0 you gain a lot of additional capabilities on top of that.

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