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
Moey
Oct 22, 2010

I LIKE TO MOVE IT
So I borrowed Spudman's script that scrapes computer name/model stuff by subnet. I am now trying to modify it to bring back much more than that. I am having a slight problem though getting memory to come back through the script.

To get back memory, I am using this.

(Get-WmiObject Win32_PhysicalMemory | measure-object Capacity -sum).sum/1mb

How do I throw that in my loop so I can assign it to a variable

I have tried a few different things, but here is the error I am getting

code:
You must provide a value expression on the right-hand side of the '-' operator.
At C:\Users\Moey\Desktop\CPUInfoNew.ps1:37 char:87
+   $mem = (Get-WmiObject Win32_PhysicalMemory | measure-object Capacity -sum).sum/1mb - <<<< Computer $ip -Credential $creds -ErrorAction SilentlyContinue
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression
Another semi-related question, any suggestions on a good Powershell book? I like automation, so I figure might as well dive into this.

Adbot
ADBOT LOVES YOU

Moey
Oct 22, 2010

I LIKE TO MOVE IT
The <<<< Computer part was added in once it errored out.

For the code, I need the | measure-object Capacity -sum as if I just do Get-WmiObject Win32_PhysicalMemory, it brings back a table of stuff that I am not interested in.


I feel like this is getting close, I am just missing something stupid.

$mem = (Get-WmiObject Win32_PhysicalMemory | measure-object Capacity -sum).sum/1mb -Computer $ip -Credential $creds -ErrorAction SilentlyContinue | ForEach-Object {$_.Capacity}

Moey
Oct 22, 2010

I LIKE TO MOVE IT

Mully Clown posted:


Have you tried the line of code from my previous post? I'm about 98% positive it will work for you.

Yup, you were correct, I am an idiot. I think I missed a chunk of that line you wrote when I copied it. Thanks. Now time to figure out all the other cool things I can add in to this.

Also, suggestions on a good PS book from anyone? I actually found this book in my office, it's almost 5 years old though.

http://www.amazon.com/Microsoft-PowerShell-Programming-Absolute-Beginner/dp/1598633546/ref=sr_1_1?ie=UTF8&qid=1314050807&sr=8-1

Moey
Oct 22, 2010

I LIKE TO MOVE IT

Clanpot Shake posted:

I'm not sure if this is the best place to ask, but I've got a question for people who know Windows scripting. We have an AD setup where about half of the users are missing a piece of required software. Rather than installing this one by one, we want to run a logon script to install and configure it using elevated privileges. We're a newly setup department so we don't have SCCM or similar to push out software yet, so this will be a one-time thing.

I started looking into this and found we could use VBScript to run commands, but I haven't been able to get it to do what I want. I've got the logic right, I'm just struggling with how VBScript handles certain things. In particular, I can't seem to get it to call the installer .exe file, but I also can't see any errors because it's running quiet.

Is this the right approach, or is there a better/easier alternative for installing software than VBScript? Are there any resources/examples of doing this?

What software are you installing? The GPO megathread in SH/SC is a good place to start for things like this. You will probably need to turn that exe into an msi, so look into something like http://www.advancedinstaller.com/

It works, but can break some installers. You can then use that with a script to check to see if it is installed, then decide if it needs to be installed or not. I have not gotten that far along with this (my environment still has users manually updating flash/java/ff, and I hate it), but that seems to be the route to go.

Moey
Oct 22, 2010

I LIKE TO MOVE IT

adaz posted:

Honestly I never trust Export-CSV anymore and just use my own custom objects added to a list then export that. I've run into more cases where it doesn't work than where it does.

This is awesome. I will probably work towards doing this for some of the things I am setting up. I finally have some free time so I started getting into my PS book. We have alot of user accounts in AD that are missing phone numbers, so I wrote/modified a little script to give me the names of these accounts. I figure next step is let it take arguments (or build it a little gui) so I can search for other empty attributes and allow it to select which OU to search instead of just the root of our users OU.

Moey
Oct 22, 2010

I LIKE TO MOVE IT
Looking for a quick pointer in the right direction. Had some success last night using powershell to do some bulk file renaming at home. Get into work today, now I actually get to try and use some of that!

I would like to try and hack through alot of this on my own, but currently I am trying to take a directory with some files in it, then go through and create a folder in that directory with the name of each file name.

Example would be I currently have c:\ps which contains a.txt b.txt c.txt

I need to then create 3 folders, c:\ps\a, c:\ps\b, c:\ps\d

Once that is created, I need to move the respective txt files into the respective folders.

Currently hung on creating the folders by the file names. I was trying something along the lines of

code:
gci |new-item -name $_.Name -itemtype "directory"
But that is complaining that c:\ps already exists.

Moey
Oct 22, 2010

I LIKE TO MOVE IT

adaz posted:

You were very close -- just need the basename (which omits the file extension) and to put the piepline input in quotes so powershell expands it before attempting to pass it to the parameter - which results in a null being passed and the new-item trying to create the root directory over again

code:
gci | new-Item -name "$_.basename" -itemtype "directory"

Didn't know that name would grab the extension as well. I am still having a problem as $_ isn't pulling files from that directory. This script will just create a folder called .basement, then error out for the rest of the items.

Moey
Oct 22, 2010

I LIKE TO MOVE IT
Thanks guys, that worked perfect. Now I am stuck on moving the files into the newly created folders.

I am trying to do something like this, but it is clearly wrong.

code:
gci | %{move-item $_ .\$_.basename.tostring()}
From the little knowledge I have, my logic is that would loop through the directory, grab the files, and then move them to the directory which name matches their file name. I am getting an error after the .tostring(

Edit

If I add in the full path, it will just renames the files without moving them. Both the lines below dont move anything, but rename both the files and the folders (appends a .basename to the end). Don't know why powershell is so difficult for me to grasp.

code:
gci | %{move-item $_ c:\ps\$_.basename}
gci | %{move-item $_ c:\ps\$_.basename\}

Moey fucked around with this message at 21:29 on Dec 21, 2011

Moey
Oct 22, 2010

I LIKE TO MOVE IT

adaz posted:

You are formatting the destination part wrong at first glance. Should be something like this

code:

gci | %{move-item -path $_.name -destination "$($_.directoryname)\$($_.basename)\$($_.name)" }

Makes sense and works! It spits out errors but still moves the files. I think it is trying to move the folders as well. If I run it on a directory with 1 file, I get 1 error. 3 files, 3 errors. Don't think it matters, not sure though.

Moey
Oct 22, 2010

I LIKE TO MOVE IT
Woop woop. Ran it today on the production data and everything worked. Could not get the moving to ignore folders, so when it hit that line, I had 1350 errors scrolling across my PS window, but it all worked out.

code:
#Renames file to append crap user wants
gci |rename-item -newname {$_.basename + " - Cat Picture.tif"}

#Creates folders based on first 9 chars
gci | %{new-item -name $_.tostring().substring(0,9) -type directory -path .}

#moves files into folders based on file number
gci | %{move-item -path $_.name -destination "$($_.directoryname)\$($_.tostring().substring(0,9))\$($_.name)" }
Thanks again everyone!

Edit: Just out of curiosity sake, how would I add "where-object {$_.psiscontainer -eq false}" to my last line of code there? Everything I try breaks it.

Moey fucked around with this message at 17:26 on Dec 23, 2011

Adbot
ADBOT LOVES YOU

Moey
Oct 22, 2010

I LIKE TO MOVE IT

Jethro posted:

Note that it's $false not false

Thanks for that. Outside the $ I was pretty close just by trial and error. I had the same thing, but I had the conditional statement (psiscontainer) after move-item.

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