PowerShell

You are currently browsing articles tagged PowerShell.

Quick script  that I put together to Shutdown my VMware Server VM, copy it to a different spot, and then start it again. It  sends me an email in the end to let me know that the script ran. Using: vmrun.

Click HERE to see.

PS: Create new user in VMWare Server for backup purposes with specific permissions. Then modify the script.

I know, I know… why don’t you just go to the Illinois Lottery website and check the numbers? Yes you can, but as I am playing with Powershell and looking for something interesting to do and research, I got this idea (It came to me after I checked my ticket and realized that I did not win anything, again.) to create a script to get all info I needed from Illinois Lottery website and send it to my email. The sending email portion is not in this piece of code, but this part you can figure out on your own :) , and you can even probably send the final output thru some SMS gateway.
You can schedule it to run on daily basis. The Illinois Lottery website is updated ~one hour after the numbers are picked.

Here is the Code.

One way of doing it.

I like the way it is done. I hope this will be useful to someone, and you can use it in a lot of ways.
You can fin it here: ConvertTo-HTML example

Good luck :)

Calling Powershell scripts (.ps1) using Batch (.bat) files. As you can tell, I have SomeScript.ps1 in the same directory as .bat file I launch.

"C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" -File "%~dp0SomeScript.ps1"

… and when I goolged, there were some other solutions as well for this… This way works for me so far.

The idea was to call any powershell script (.ps1),  located in the same folder as that .vbs script:

Code:

Dim sCurPath
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
'WScript.Echo sCurPath

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe –noexit &'" & sCurPath & "\SomeScript.ps1'")

I hope it helps someone.

Something new got created in PowerShell 2.0 for users who create scripts. It is called PowerShell Integrated Scripting Environment (ISE) and you can find executable where you have powershell installed, “powershell_ise.exe” and I found it in “C:\WINDOWS\system32\windowspowershell\v1.0″.
After you start it, you will see window looking like this:
ps20ise

Someone already online created some aliases that you can add to your powershell profile, so when you type let’s say: “ise” in powershell it will open ISE.
So yeah, if you google it, you will find somewhere a note on that code:

function Invoke-ISE{
param (
[string[]]$file,
[string]$path="$pshome\powershell_ise.exe"
) 

if($file){
foreach($f in $file){
   & $path (resolve-path $f)
}
} else {
   & $path
}
}

Set-Alias ISE Invoke-ISE

From Microsoft site.
From a blog :)

Have fun using ISE.

Self explaining piece of code:

#———————————————————————————————————————————–
#Email options for automated emailed report
$smtpServer = “YOUR SMPT SERVER ADDRESS”
$txtFilePath = “c:\somefile.txt”
#
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($txtFilePath)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = “Some Email Address”
$msg.To.Add(”Some Email Address”)
$msg.Subject = “Some Subject”
$msg.Body = “Some Body Data”
$msg.Attachments.Add($att)

$smtp.Send($msg)
#———————————————————————————————————————————–

Here is a list of special characters.

* `O – inserts a null value
* `a sends an alert (bell or beep) to the computers speaker
* `b inserts a backspace
* `f inserts a form feed
* `n inserts a new line
* `r inserts a carriage return
* `t inserts a horizontal tab
* `v inserts a vertical tab
* `’ inserts a single quote
* `” inserts a double quote

Yeah, I am going crazy sometimes, because you change it once, and after a few months you forget how it’s done, and you try to find the right command again….
So… just note for me… link to a website with some good explanation:

Signing PowerShell Scripts

I needed some software/script to shutdown group of chosen computers at a time specified in windows scheduler. I Googled …. and after some research I put this script together:

$erroractionpreference = “SilentlyContinue”

$psSwitches = ” -s -f -c -t 30″
$psMessage = ” -m” + ‘ “Please press CANCEL button if you want to CONTINUE to work. DO NOT forget to shutdown your computer before you leave”‘

$strComputer = get-content “computers.txt”
$application = “psshutdown.exe”

Foreach ($i in $strComputer)
{
$arguments = “\\$i” + $psSwitches + $psMessage
[System.Diagnostics.Process]::start($application,$arguments)
# write-host $arguments
}

As you can see, I used one of the PSTools applications to do it :)
Powershell has it’s own ways to perform the task using Win32Shutdown Method of the Win32_OperatingSystem Class, and I will try it next time… ;)

I want to mention that Ying Li created some scripts similar to this one. I just modified them to fit my needs.

I use Group Policy to add and remove Groups from Local Administrator Group, however something happened and when we modified our Group Policy to remove “Domain Users” Group(after some testing), it did not happen. We looked thru other GPOs and could not find anything. When we manually removed from test computer the “Domain Users” group from Local Adminsitrators group and we issued the command “gpupdate /force” and rebooted, the “Domain Users” group was not added back so definitelly it was not a policy.

So it was a weird behaviour and I decided to look for some VB scripts to do this for me, but as I started to learn PowerShell I decided to do some research and  at the end I came up with this script.

$erroractionpreference = “SilentlyContinue”

$domain = “yourdomain”
$username = “Domain Users”

$strComputer = get-content “computer_list.txt”

Foreach ($i in $strComputer)
{    $computer = [ADSI](“WinNT://” + $i + “,computer”)
$computer.name

$Group = $computer.psbase.children.find(“administrators”)
#    $Group.name
$Group.Remove(“WinNT://” + $domain + “/” + $username)

}

That’s it :)

I think you are going to like it:

http://blogs.msdn.com/powershell/archive/2006/04/25/583241.aspx

Note for myself:

$erroractionpreference = “SilentlyContinue”

Locations of visitors to this page