More Auto-Loading, Custom PowerShell Scripts

In this update, you’ll see how I added two or three new scripts to the environment and also changed how most of the scripts work.

One new script that I add was a script that would take CSV data and add the users to a group in Active Directory. You can see this below:


function Get-CSVAddToGroup{
<#
.SYNOPSIS
This function allows you to add usernames from a CSV to a specified Active Directory group.
It needs confirmation for each addition to a group.

.EXAMPLE
Get-CheckCSVAddToGroup -Groupname "all rossendale staff" -path c:\testing\test.csv

#>
[cmdletbinding()]
param(
[parameter(Mandatory=$true)]
[string]$groupname,

[parameter(mandatory=$true)]
[string]$path
)

$csvformembership = Import-Csv -Path $path

foreach ($b in $csvformembership){
$name = $b.user
Add-ADGroupMember -Identity "$groupname" -Members $name -Confirm
}
}

Another script I added was my script for sending messages to remote computers on the network. You can see this below:


function Send-Message{

function send-messagewithname{
$name = Read-Host "Enter a name"
$msg = Read-Host "enter a message"
$adcompstocheck = Get-ADComputer -LDAPFilter "(name=$name)" -Properties name

if ($adcompstocheck -eq $null){
Write-Host "$name doesn't exist!"
pause
}else{
Invoke-WmiMethod -Path win32_process -Name create -ArgumentList "msg * $msg" -ComputerName "$name"
}
}

function send-messagewithip{
$ip = Read-Host "enter an IP"
$msg = Read-Host "enter a message"
$adcompstocheckip = Get-ADComputer -Filter * -Properties ipv4address, name | select ipv4address
if ($adcompstocheckip -eq $null){
write-host "$ip doesn't exist"
pause
}else{
Invoke-WmiMethod -Path win32_process -Name create -ArgumentList "msg * $msg" -ComputerName "$ip"
}
}

do {$nameorip = Read-Host "Do you want to use IP or Name (I or N)?"} while (("i","n") -notcontains $nameorip)
if ($nameorip -eq "n"){
send-messagewithname
}else {
send-messagewithip
}
}

Finally, my last script that was added to my custom PowerShell environment allowed me to list all the computers on the network with a specific operating system. For example “2012” or “Professional”. The code for this is below;


function list-OSVersonComputers{
<#
.SYNOPSIS
This function allows you to list all of the computers with the requested OS version.

.EXAMPLE
List-OSVersionComputers -OSVersion 2012

.EXAMPLE
List-OSVersionComputers -OSVersion standard
#>
[cmdletbinding()]
param(
[parameter(mandatory=$true)]
[string]$OSVersion
)

$OSVersion2 = '*' + ($OSVersion) + '*'

Get-ADComputer -Filter {OperatingSystem -like $OSVersion2} -Property * | Format-Table name,operatingsystem,lastlogondate -Wrap -AutoSize
}

I also added help menus to most of my scripts to make them more professional.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s