Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Tuesday, September 20, 2011

Powershell Get-ChildItem Count 1 result

This is going to be a quick one. I'm working on a new Powershell script (my second) and I'm doing some sanity checks before I actually try to continue the workflow.

If you run the command below and it returns only one item and then either try to print out $fileCount or run an if statement or something against it like if ($fileCount -gt 0){do some stuff} the "do some stuff" won't happen.
#$fileCount = $(get-childitem C:\ -filter *.zip).count

This is because the count method returns an array, which if only one item is in it doesn't get created and thus is 0.   To get around this you must create the array first. You can do this in the same line of code as shown below.

#$fileCount = @(get-childitem C:\ -filter *.zip).count

The at sign there will create the array and you will have one item in it.


Thursday, October 14, 2010

Using PSExec to Defragment your PCs

Way back in 2000 / 2001 I was an IT intern. My job was to do the really manual processes for a small department with in a much larger company. One of those was monthly defrags of all PCs and Laptops (if they where available). I had to go to each and every PC, log on, and start the defrag process manually. I believe this to be my first moment of "I should automate this". Problem was I didn't know how or really where to start. I knew script was the answer, but just couldn't get things to work. I believe I eventually created a scheduled task on each machine (by hand) to do this for us.

Fast forward 10 years and I am still in need of the same thing, as we don't have Vista or Windows 7 deployed. But now I have a much better understanding of what needs to happen and even better I know where to start!

PSExec, which is part of the System Internals PsTools suite is my answer these days. A simple script, run from my PC (still manually for the time being) is able to handle defragmenting all of our PCs.

psexec @C:\Updates\Comps\AppPCs.txt -n 10 -c -f -d JkDefragCMD.exe

I use the files with PC names to speed up re-deployment of everything. The -n 10 flag tells psexec t0 wait 10 seconds before it times out the PC, instead of 60 (I believe this is the default). -c copies the file (jkdefragcmd.exe) to the remote system. The flag -f forces copying, even if the file exists. The flag -d doesn't wait for the process to terminate, this is as asynchronously as I can do.

My next step is to hook this up to task scheduler and have it run the first Sunday or something.

Tuesday, July 14, 2009

Ghostscript Convert PDF to TIFF

Again this is mainly for my reference, but someone may have trouble finding the solution like I did earlier today.

On Windows be sure to add "C:\Program Files\gs\gs8.64\bin" to your PATH, then run the following command.

gswin32c -dNOPAUSE -q -r300 -sDEVICE=tiffg4 -dBATCH -sOutputFile=[YOUR_OUTPUT_FILENAME.TIFF] [YOUR_INPUT_FILENAME.PDF]

Similarly on Linux you can run the command below.

gs -dNOPAUSE -q -r300 -sDEVICE=tiffg4 -dBATCH -sOutputFile=[YOUR_OUTPUT_FILENAME.TIFF] [YOUR_INPUT_FILENAME.PDF]

There are also possibilities to script this, and when you can do that you should. I haven't found the need to bring it to that point yet as I just had to do four files today, but should this come up again I will be scripting it in some fashion and posting the results here.

Thank you to StackOverflow for the solution to my problem. Also one of the other solutions to this is to use a recursive search of a directory with PowerShell to convert files.

Friday, January 23, 2009

Things learned over time, Part 1

This is going to be an on going series of quick posts.  Some of those quick little tid bits of information that once you know you never forget, but up until that point it makes things harder than they need to be.

I typically change directories to where I want files to end up if say I am using wget or xcopy.  I used to do xcopy z:\some\directory\* c:\Users\Steve\Desktop\Directory 

now I have either realized or learnd from someone that you can do 

xcopy z:\some\directory\* .

Saves a lot of typing and reduces mistakes.  Again you have to be in the directory for it to work.
Alternatively if you open a command prompt by default you are in your home directory, so if I wanted the same outcome but didn't want to change directories I would do 
xcopy z:\some\directory Desktop\Directory

Of course this works on Linux as well as Windows but the paths and slashes will be different.

Thursday, October 23, 2008

Powershell: Equal Sign vs. -eq

I was just working on a script in powershell and saw the error "Length" is a ReadOnly property. It was from the code if ($args.count = 0) {exit;}.  Having "grown up" writing VB code this would have worked.  But in powershell you need to do if ($args.count -eq 0) {exit;} instead.

Powershell: Accept Unknown number of Args and process them

Recently I needed a script to move some files for a user.  It might be two or three files but I wanted to exersise my noggin in powershell.  This code will take any number of arguments and copy the files from the input directory to the output directory. Below is the code.

::Start Code::
# Display Help
if (($Args[0] -eq "-?") -or ($Args[0] -eq "-help")) {
   ""
   "Usage: pullQualityAssesments.ps1 "
   "            copies the Faxjob over to the Quality Assesments folder."
   ""
   "Example: pullQualityAssesments.ps1 400001 400002"
   ""
   exit
}
$inputdir = "\\someserver\savedfaxjobs\"
$outputdir = "\\someotherserver\Quality Assessments\"
$x = $args.count
$i = 0
do {
   $ordernumber = $args[$i]
   #write-host $inputdir$ordernumber.faxjob.* $outputdir
   $filelist = get-childitem $inputdir$ordernumber.faxjob.*
   foreach ($file in $filelist){write-host $file}
   $i++}
while ($i -le $x)
::End Code::

Unfortunatly we have 23,000+ files in our Input directory and this script is slower than me pulling them manually.  Maybe someday I can revisit this to filter it down to something that works and pulls in only the files it needs.

Powershell Check MySQL Status

The script below will check the specified server using the credentials provided and report back to you all of the Status Variables. You will have to change the line "$myconnection.ConnectionString" to what ever your settings are.  On a side note drop the <> from the lines.  Also you will need the MySQL .Net Connector for this to work.


::Start Code::
[void][system.reflection.Assembly]::LoadFrom("C:\Program Files\MySQL\MySQL Connector Net 5.2.3\Binaries\.NET 2.0\MySQL.Data.dll")

$myconnection = New-Object MySql.Data.MySqlClient.MySqlConnection
$myconnection.ConnectionString = "server=;user id=;password=;pooling=false"
$myconnection.Open()

$mycommand = New-Object MySql.Data.MySqlClient.MySqlCommand
$mycommand.Connection = $myconnection
$mycommand.CommandText = "SHOW STATUS;"
$myreader = $mycommand.ExecuteReader()
while($myreader.Read()){write-host $myreader.getstring(0)"|"$myreader.getstring(1)}
$mycommand.Connection.close()
::End Code::

If you are only interested in a two or three of those variables (say threads_running and threads_connected) you can use the code below.

[void][system.reflection.Assembly]::LoadFrom("C:\Program Files\MySQL\MySQL Connector Net 5.2.3\Binaries\.NET 2.0\MySQL.Data.dll")

$myconnection = New-Object MySql.Data.MySqlClient.MySqlConnection
$myconnection.ConnectionString = "server=IP OR HOSTNAME;user id=YOUR_USER;password=YOUR_PASSWORD;pooling=false"
$myconnection.Open()

$mycommand = New-Object MySql.Data.MySqlClient.MySqlCommand
$mycommand.Connection = $myconnection
$mycommand.CommandText = "SHOW STATUS LIKE 'threads_connected';"
$myreader = $mycommand.ExecuteReader()

while($myreader.Read()){write-host $myreader.getstring(0)" : "$myreader.getstring(1)}
$mycommand.Connection.close()
$mycommand.connection.open()
$mycommand.CommandText = "SHOW STATUS LIKE 'threads_running';"
$myreader = $mycommand.executereader()
while($myreader.read()){write-host $myreader.getstring(0)" : "$myreader.getstring(1)}
$mycommand.Connection.close()