Drive Name Checker Against Variable

Before you can use scripts, see my post on how to allow them to be run here

This script gets the name of a drive specified by the drive letter and then compares this to a variable. In my script I compared the drive to the day of the week and the actual name of the drive.

First we need to get the name of the drive and put it into a variable. This is the code I used:

 $drive = Get-Wmiobject -Class win32_volume -Filter "Driveletter = 'D:'" 

Now we need a something to compare the drive name to. Here you can see that I have used both the day of the week and the actual name of the drive. I did this because you might have daily back-ups and want to make sure that the right drive is inserted for that day. Here’s the code I used:

 $compareto = ( "Back-Up Drive" ) 

or you could compare the drive name to the day of the week:

 $compareto = ( (Get-Date).DayOfWeek ) 

Here is the actual function that compares the drive name to the variable “$compareto” that we just created:


function usb-check {

if ( (($drive).Label -eq $compareto) ){

Write-Host "The drive is the same as the variable"

}elseif ( (($drive).Label -eq $compareto) ){

Write-host "The drive is NOT the same as specified"

}}

Here is the full code for you to use:


$drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'D:'"
$compareto = ( "Back-Up Drive")
## ( (Get-Date).DayOfWeek )
##

function usb-check {
if ( (($drive).Label -eq $compareto) ){
Write-Host "The drive is the same as specified"
}elseif ( (($drive).Label -ne $compareto) ){
Write-Host "The drive is not the same as specified"

}
}

usb-check

pause

You can see that I have two variables, one commented out and another in use. This is just to give me and you more options to switch between quickly. If your wondering below are the two outputs from the function:

1

2

Hope you enjoyed!

 

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