Wednesday, January 11, 2012

My First Powershell script

Seems that Powershell is the way to go forward as it is well supported with Windows 7 and Microsoft. It is a powerful script. I think it will revolutionize the way we script today.
I just wrote my first powershell script and thought it would be good to share with all. I am not going to give any tutorial for Powershell as there are a lot of them available.
There are a few things which are worth noting in this script:

1) You can get the script directory from the below script in Get-ScriptDirectory function.
2) You can suppress the remote exe installation prompt by command: $env:SEE_MASK_NOZONECHECKS = 1
code
Remove-Item env:\SEE_MASK_NOZONECHECKS
3) Installation of setup.exe with parameters can be seen in function InstallPackage
4) Permissions can be set in Powershell script using the GivePermissions function below.


Here is the code:
------------------------------------------------------

function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$env:SEE_MASK_NOZONECHECKS = 1
$val1 = Get-ScriptDirectory
$CurrentPath = $val1.ToString()
$Setup = $CurrentPath + "\setup.exe"
$Config = " " 
function InstallPackage
{
# Write-host $Setup
# Write-host $Config
    $process = [Diagnostics.Process]::Start($Setup,$Config)
    $process.WaitForExit()
}
function GivePermissions
{
$acl = Get-Acl "C:\Program Files\PLSQL"
$acl.SetAccessRuleProtection($True, $True)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Users","Modify", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl.AddAccessRule($rule)
Set-Acl "C:\Program Files\PLSQL" $acl
}
# -Main ----------------------------------
InstallPackage
GivePermissions
Remove-Item env:\SEE_MASK_NOZONECHECKS

No comments: