How to run / execute powershell commands in excel vba - vba

I’m writing some code to compare installed versions of software in some test computers currently I’m using a PowerShell PS1 script to create a text file and compare with previously created baseline text file
For the ease of end users I would like to automate these in a excel file, press a button and you get a result of what does not match with the baseline
My current ps1 scripts are
:CreateDefaultApps.ps1 - Create the Default baseline DefApp32.txt or DefApp64.txt in My Documents folder
cls
$CurDir = $myinvocation.InvocationName | split-path -parent
$DOCDIR = [Environment]::GetFolderPath("MyDocuments")
$TARGETDIR = "$DOCDIR\AppLog"
$COMPNAME = $env:computername
if(!(Test-Path -Path $TARGETDIR ))
{
New-Item -ItemType directory -Path $TARGETDIR
}
cls
if ([Environment]::Is64BitOperatingSystem)
{
Write-Host "64bit Windows Detected"
Write-Host "Collecting Product Information for"$COMPNAME
If (Test-Path $TARGETDIR\DefApp64.txt)
{
Remove-Item $TARGETDIR\DefApp64.txt
}
Else
{
Get-WmiObject -Class Win32_Product | Select-Object -Property Name, Version > $TARGETDIR\DefApp64.txt
}
write-host "File"$TARGETDIR"\DefApp64.txt Created"
}
else
{
Write-Host "32bit Windows Detected"
Write-Host "Collecting Product Information for"$COMPNAME
If (Test-Path $TARGETDIR\DefApp32.txt)
{
Remove-Item $TARGETDIR\DefApp32.txt
}
Else
{
Get-WmiObject -Class Win32_Product | Select-Object -Property Name, Version > $TARGETDIR\DefApp32.txt
}
write-host "File " $TARGETDIR"\DefApp32.txt Created"
}
:CompareApps.ps1 creates current list of applications and compare with Baseline
cls
$CurDir = $myinvocation.InvocationName | split-path -parent
Function Abort
{
Write-Host "Script Aborted"
}
$DOCDIR = [Environment]::GetFolderPath("MyDocuments")
$TARGETDIR = "$DOCDIR\AppLog"
$COMPNAME = $env:computername
if(!(Test-Path -Path $TARGETDIR ))
{
Write-Host $TARGETDIR" Folder does not Exist"
Abort
}
Function Finish
{
write-host "Comparing Products Completed"
}
Function CreateCompNameFile
{
Get-WmiObject -Class Win32_Product | Select-Object -Property Name, Version > $TARGETDIR\$COMPNAME.txt
write-host "File"$TARGETDIR\$COMPNAME".txt Created"
}
Function Compare64
{
write-host "Comparing Products with file"$TARGETDIR\$COMPNAME".txt"
Compare-Object -ReferenceObject (Get-Content $TARGETDIR\DefApp64.txt) -DifferenceObject (Get-Content $TARGETDIR\$COMPNAME.txt)
Finish
}
Function Compare32
{
write-host "Comparing Products with file"$TARGETDIR\$COMPNAME".txt"
Compare-Object -ReferenceObject (Get-Content $TARGETDIR\DefApp32.txt) -DifferenceObject (Get-Content $TARGETDIR\$COMPNAME.txt)
Finish
}
cls
if ([environment]::Is64BitOperatingSystem)
{
Write-Host "64bit Windows Detected"
Write-Host "Collecting Product Information for"$COMPNAME
If (Test-Path $TARGETDIR\$COMPNAME.txt)
{
Remove-Item $TARGETDIR\$COMPNAME.txt
CreateCompNameFile
}
else
{
CreateCompNameFile
}
Compare64
}
else
{
Write-Host "32bit Windows Detected"
Write-Host "Collecting Product Information for"$COMPNAME
If (Test-Path $TARGETDIR\$COMPNAME.txt)
{
Remove-Item $TARGETDIR\$COMPNAME.txt
CreateCompNameFile
}
else
{
CreateCompNameFile
}
Compare32
}
I would prefer to run this code via excel and capture the list in a worksheet, then compare the baseline which is already in the worksheet. How do I execute or use commands from PowerShell in VBA?
Previously I have used (see post How to list all installed applications in to excel)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & StrComputer & "\root\cimv2")
Set objAllSoftwares = objWMIService.ExecQuery("Select * from Win32_Product")
For some reason it does not retrieve all the software installed (which you can see in Add/Remove Programs) e.g. 7-Zip (64 bit), GPL Ghost Script to name a few
Any help would be much appreciated.
Thanks
Dumidu

Related

Creating shortcut dynamically in powershell

#Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
#step 1) Installing Chocolatey when needed
if (-not (Test-Path -Path "$env:ProgramData\Chocolatey\choco.exe" -PathType Leaf))
{
# from https://chocolatey.org/install
Set-ExecutionPolicy Bypass -Scope Process -Force;
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
}
# Step 2) defining the array of packages which we want to install
$Packages = 'googlechrome',
'firefox',
'codeblocks',
'windbg',
'x64dbg.portable',
'visualstudio2019community',
'nasm',
'explorersuite',
'pestudio',
'vscode',
'sysinternals',
'python',
'ccleaner',
'anaconda3',
'wireshark',
'sublimetext3',
'notepadplusplus',
'ida-free'
# Step 3) defining the Show-Menu function
function Show-Menu
{
Clear-Host
Write-Host "**********************************************"
Write-Host "LIST OF SOFTWARES"
# writing the options using the array of packages
for ($i = 0; $i -lt $Packages.Count; $i++)
{
# {0,10} means right align with spaces to max 10 characters
Write-Host ('{0,10}. {1}' -f ($i + 1), $Packages[$i])
}
Write-Host " q. Exit the script"
Write-Host "*************************************************"
Write-Host
}
# Step 4) enter an endless loop you only exit if the user enters 'q'
while ($true)
{
Show-Menu
$UserInput = Read-Host "Select the softwares number(s) to be installed (space separated)"
# testing if the user wants to quit and if so, break the loop
if ($UserInput -eq 'q')
{
break
}
foreach($input in $UserInput.Split(' '))
{
# testing if the user entered a number between 1 and the total number of packages (inclusive)
if ([int]::TryParse($input,[ref]$null) -and 1..$Packages.Count -contains [int]$input)
{
# here you install the chosen package using the array index number (= user input number minus 1)
$packageIndex = [int]$input - 1
Write-Host "Installing $($Packages[$packageIndex])"
Choco install $Packages[$packageIndex] -y --ignore-checksums
############## Creating Desktop shortcut ######################
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Desktop\ccleaner.lnk")
$Shortcut.TargetPath = "C:\Program Files\WIndowApps\ccleaner\ccleaner.exe"
$Shortcut.Save()
}
else
{
$availableOptions = 1..$Packages.Count -join ','
Write-Host "Error in selection, choose $availableOptions or q" -ForegroundColor Red
}
}
$null = Read-Host "Press Enter to continue"
}
I have created desktop shortcuts of the software to be installed but instead of writing manually for every application as filename.lnk is their any other way of doing so that as soon as software is installed software shortcut has to created on desktop.
Creating separate function for shortcut and calling the shortcut function

Remove permissions on a sharepoint sitecollection

So I have a list of sitecollections and with them everyone who have access to the sitecollections. A ; separated csv file, formated like /sites1/sites;AD\User1.
We need to make a cleanup on ~400 sites where we want to remove users beloning to a certain company or being in a certain AD.
I have this information so thats not a problem.
Is there a way to make a powershellscript to remove theese users on theese site collection, in the other output we don't have any list of where they have permission only that they have it somewhere on the site.
I would also need the output format in a good way so we can restore them in a case it would be needed?
Can this be done?
We used below to get the list of users:
$ver = $host | select version
if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}
$pathSave = "D:\Script\Output.csv"
$pathRead = "D:\Script\\sites.txt"
write-output = "======================================================================="
[System.Collections.ArrayList]$objectCollection = #();
[System.Collections.ArrayList]$allUsersArray = #();
foreach($line in Get-Content $pathRead)
{
$site = Get-SPSite($line)
$sitecntr = 0;
#write-output " ";
write-output $site.Url;
$allusers = $site.RootWeb.AllUsers;
foreach ($user in $allusers)
{
if (!$user.IsDomainGroup)
{
$sitecntr++;
$outstring = $site.Url + ";" + $user.UserLogin + ";" + $user.Email
$a = $allUsersArray.Add($outstring);
}
}
$outString = "Totalt: " + $sitecntr
if ($sitecntr -ne 0) {
write-output $outstring;
write-output " ";
}
$site.Dispose();
}
write-output ""
write-output "Exporting to csv..."
Out-File -FilePath $pathSave -InputObject $allUsersArray
write-output "Finished! "
write-output ""
This script will ensure reader permissions to the specified user across the site collection.
**Keep in mind:** This is not the right way to deal with permissions issues!
$site = Get-SPSite -Identity "http://mysite/"
$user = Get-SPUser -Identity "mydomain\myuser" -Web $site.RootWeb
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($user)
$role = $site.RootWeb.RoleDefinitions[[Microsoft.SharePoint.SPRoleType]::Reader]
$assignment.RoleDefinitionBindings.Add($role);
foreach ($web in $site.AllWebs) {
if ($web.HasUniquePerm) {
$web.RoleAssignments.Add($assignment)
}
}
Note: You need to be Site Collection Admin to use this script.

Disable Downloading Cached FTP File

I've got a PowerShell script that I call from VBA using Excel. The script uses WinSCP to download some datetime-named FTP and SFTP files and saves them with a static filename, overwriting the old file, on a network drive location.
The script works on first run, but after that it loads the same cached version of the file. The workaround is to change the cache settings in IE to check for newer versions of stored webpages 'every time I visit the webpage'.
The macro is used by several people and is accessed using a variety of computers. Is there a way around this that I can incorporate in my code, either in VBA or PS so they don't have to remember to go into IE to change their settings?
Script is called from VBA:
Call Shell("powershell -executionpolicy bypass & ""H:\FTP\FTP.ps1""", vbHide)
Script:
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$localPath = "H:\Worksheets\FTP"
$remotePath = "/outgoing/data/LatestData/"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::ftp
$sessionOptions.HostName =
$sessionOptions.UserName =
$sessionOptions.Password =
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Get list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)
# Select the most recent file
$latest = $directoryInfo.Files |
Where-Object { -Not $_.IsDirectory} |
Where-Object {
[System.IO.Path]::GetExtension($_.Name) -eq ".nc1" -or
[System.IO.Path]::GetExtension($_.Name) -eq ".ky1" -or
[System.IO.Path]::GetExtension($_.Name) -like ".tn*" }
Group-Object { [System.IO.Path]::GetExtension($_.Name) } |
ForEach-Object{
$_.Group | Sort-Object LastWriteTime -Descending | Select -First 1
}
$extension = [System.IO.Path]::GetExtension($latest.Name)
"GetExtension('{0}') returns '{1}'" -f $fileName, $extension
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}
$latest | ForEach-Object{
$extension = ([System.IO.Path]::GetExtension($_.Name)).Trim(".")
$session.GetFiles($session.EscapeFileMask($remotePath + $_.Name), "$localPath\$extension.txt" ).Check()
}
$stamp = $(Get-Date -f "yyyy-MM-dd-HHmm")
$filename = $stamp.subString(0,$stamp.length-6)
$session.GetFiles(
($remotePath + $fileName),
($localPath + $fileName + "." + $stamp)).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}

Variable won't store data in powershell

For some reason I can't get this variable ($SelectedDrive) to store data in it using the Get-Content cmdlet.
I have created a function as shown below:
#Allow user to select which drive to backup to and checks that the user hasn't canceled out of the Drive Selection GUI
Function SelectDrive{
ShowUSBs | Out-File $locBackupFolder\RemovableStorage.txt
Get-Content $locBackupFolder\RemovableStorage.txt | Out-GridView -OutputMode Single -Title "Choose USB Storage Device" | Out-File $locBackupFolder\Drive.txt -Encoding default
$SelectedDrive = Get-Content $locBackupFolder\Drive.txt
IF ($SelectedDrive -eq $null) {
WarningCancel
RemoveBackupDrive
Exit
} ELSE {
$BackupDrive = $SelectedDrive.Substring(0,2)
}
}
When I run the script in Powershell ISE using the F5 button it runs through, and then fails to enter the data.
I go back and check it over and can't see any issue, as it works if I run it piece by piece using Run Selection.
The whole script is below:
######### DEFINE FUNCTIONS #########
$locBackupFolder = "C:\Backup"
#Check Backup Drive
IF (!(Test-Path $locBackupFolder)) {
#C:\Backup does NOT exist
New-Item $locBackupFolder -ItemType Directory
}
#Inform user only D:\Username will be backed up
function WarningDialog(
$MessageWarning = "!!!WARNING!!!
YOU MAY LOSE DATA IF YOU DO NOT ADHERE TO THIS MESSAGE
Only D:\$env:USERNAME will be backed up, please ensure any Data that is sitting in the root of D:\ is moved to D:\$env:USERNAME
FYI: Your desktop data is safe and will be backed up, you need only worry about data in the root of D:\ or anything you have stored in
C:\ Drive.
FURTHER MORE Outlook will shutdown by itself through this process, please do not open it up again until everything is finished.
If you have data to move, please click Cancel now, otherwise please press OK to continue the backup procedure.
For any help, please see your IT Technicians, or call
off-site.",
$WindowTitleWarning = "Backup your data from your laptop",
[System.Windows.Forms.MessageBoxButtons]$ButtonsWarning = [System.Windows.Forms.MessageBoxButtons]::OKCancel,
[System.Windows.Forms.MessageBoxIcon]$IconWarning = [System.Windows.Forms.MessageBoxIcon]::Stop
)
{
Add-Type -AssemblyName System.Windows.Forms
return [System.Windows.Forms.MessageBox]::Show($MessageWarning, $WindowTitleWarning, $ButtonsWarning, $IconWarning)
}
#Checks to see if logged on user has a D:\USERNAME directory and if not, informs them to see IT for a custom backup.
Function TestUserDrive {
IF (!(Test-Path "D:\$env:USERNAME")){
#D:\USERNAME does NOT exist
}
}
#Displays an instruction/how-to of how to move data from the root of D:\ to the users D:\USERNAME folder
function WarningCancel(
$MessageCancel = "INSTRUCTIONS:
You have chosen to cancel the backup script, if this is due to requiring data to be moved to inside your D:\$env:USERNAME folder, please do the following.
1. Open My Computer
2. Double click on Data (D:) to open your D:\ Drive
3. Move or Copy anything from this directory that you wish to keep, into the directory called $env:USERNAME\My Documents
4. Once this has been completed, re-run this script to commence the backup procedure again
Easy as that!
For any help, please see your IT Technicians, or call
off-site",
$WindowTitleCancel = "Backup your data from your laptop",
[System.Windows.Forms.MessageBoxButtons]$ButtonsCancel = [System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]$IconCancel = [System.Windows.Forms.MessageBoxIcon]::Information
)
{
Add-Type -AssemblyName System.Windows.Forms
return [System.Windows.Forms.MessageBox]::Show($MessageCancel, $WindowTitleCancel, $ButtonsCancel, $IconCancel)
}
#Informs the user to select the device they would like to backup to when the selection box is displayed
function SelectDevicePrompt(
$MessageSelect = "On the next screen please specify the device you would like to backup your data to.
The devices you currently have plugged in will show, please select your chosen device, and then click the OK button at the bottom right of the window.",
$WindowTitleSelect = "Backup your data from your laptop",
[System.Windows.Forms.MessageBoxButtons]$ButtonsSelect = [System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]$IconSelect = [System.Windows.Forms.MessageBoxIcon]::Hand
)
{
Add-Type -AssemblyName System.Windows.Forms
return [System.Windows.Forms.MessageBox]::Show($MessageSelect, $WindowTitleSelect, $ButtonsSelect, $IconSelect)
}
#Displays a list of all removable storage devices volume names and their allocated drive letter
Function ShowUSBs{
$USBInfo = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"}
$USBInfo | Format-Table `
#{Expression={$_.DeviceID};Label="Drive Letter";Width=25}, `
#{Expression={$_.VolumeName};Label="USB Name";Width=20}
}
#Allow user to select which drive to backup to and checks that the user hasn't canceled out of the Drive Selection GUI
Function SelectDrive{
ShowUSBs | Out-File $locBackupFolder\RemovableStorage.txt
Get-Content $locBackupFolder\RemovableStorage.txt | Out-GridView -OutputMode Single -Title "Choose USB Storage Device" | Out-File $locBackupFolder\Drive.txt -Encoding default
$SelectedDrive = Get-Content $locBackupFolder\Drive.txt
IF ($SelectedDrive -eq $null) {
WarningCancel
RemoveBackupDrive
Exit
} ELSE {
$BackupDrive = $SelectedDrive.Substring(0,2)
}
}
$BackupDrive
#Imports list of active processes and looks for outlook process then kills it if found
function KillOutlook{
$processactive = Get-Process
IF($processactive.ProcessName -contains "Outlook") {
Stop-Process -Name Outlook
Start-Sleep 1
$OLcheckagain = Get-Process
IF($OLcheckagain.Processname -contains "Outlook") {
Write-Host "Outlook failed to close"
}
} Else {
Write-Host "Outlook is closed"
}
}
#Find the pst files attached to outlook and output the values to C:\Backup\PST.txt
function FindPSTs{
$outlook = New-Object -comObject Outlook.Application
$pstloc = $outlook.Session.Stores | where { ($_.FilePath -like '*.PST') }
$pstloc.FilePath | out-file -FilePath "$locBackupFolder\PST.txt" -Encoding Default
}
#Removes C:\Backup Directory
Function RemoveBackupDrive {
IF (Test-Path $locBackupFolder){
Remove-Item $locBackupFolder -Force -Recurse
}
}
#Copy data from D:\USERNAME to BackupDrive
Function CopyData {
IF (!(Test-Path $BackupDrive)) {
robocopy D:\$env:USERNAME $BackupDrive /MIR /COPYALL /r:03 /w:5 /MT:9
} ELSE {
robocopy D:\$env:USERNAME $BackupDrive /
}
}
#Copy PST files explicitly to BackupDrive\AppData\Roaming\Email
Function CopyPST {
KillOutlook
Start-Sleep 1
IF (!(Test-Path $BackupDrive\AppData\Roaming\Email)) {
New-Item $BackupDrive\AppData\Roaming\Email -ItemType Directory
}
Get-Content $locBackupFolder\PST.txt | ForEach-Object {
IF (Test-Path $_){
Copy-Item $_ "$BackupDrive\AppData\Roaming\Email"
}
}
}
######### START SCRIPT #########
#Display warning to inform user that only D:\USERNAME will be backed up
$WarningAccept = WarningDialog
#If cancel is selected from WarningDialog, then display WarningCancel message pop-up giving instructions to backup data from D:\ to D:\USERNAME
IF ($WarningAccept -eq "Cancel") {
WarningCancel
RemoveBackupDrive
Exit
}
#Prompts user to select Device to backup to
SelectDevicePrompt
#Shows the selection page for the user to select the device
SelectDrive
#Find the pst files attached to outlook and output to C:\Backup\PST.txt
FindPSTs
#Inform user where their data will be backed up to
Write-Host "Your data will be backed up to $BackupDrive"
#If Outlook is found, stop its process, otherwise continue
KillOutlook
#Running backup of everything in D:\ drive using robocopy
#If this is the first time copying all data /MIR will be used, otherwise if the directory DRIVE:\USERNAME\Backup exists
#robocopy will not /MIR and will only update newly added data.
CopyData
#Copy PST files specifically
#CopyPST
If I can be of any further assistance, please let me know.
I think your problem is simply that the SelectDrive function doesn't return anything. $backupDrive is local to the function, so you cannot refer to it from outside the function.
Also converting everything to text is going to introduce other bugs, e.g. you can select any line from the grid view including your headers and the blank lines top and bottom. Some better code might be:
Function GetUSBs{
$USBInfo = gwmi win32_diskdrive |
?{$_.interfacetype -eq "USB"} |
%{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |
%{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"}
Write-Output $USBInfo | select #{n="Drive Letter";e={$_.DeviceID}},#{n="USB Name";e={$_.VolumeName}}
}
Function SelectDrive{
$usbs = GetUSBs
$usbs | Format-Table -AutoSize | Out-File $locBackupFolder\RemovableStorage.txt
$selectedDrive = $usbs | Out-GridView -OutputMode Single -Title "Choose USB Storage Device"
$selectedDrive | Out-File $locBackupFolder\Drive.txt -Encoding default
if ($SelectedDrive -eq $null) {
WarningCancel
RemoveBackupDrive
exit
} else {
write-output $selectedDrive.'Drive Letter'.Substring(0,2)
}
}
$backupDrive = SelectDrive
This version still writes to your two files, but it doesn't read either of them, instead it keeps the objects internally. This means you get a better display in the grid view as the grid's own column headings are used, and you don't get any selectable lines you don't want.
There's a lot going on in your script, I debugged through it myself and found that these lines are causing the problems
ShowUSBs | Out-File $locBackupFolder\RemovableStorage.txt
Get-Content $locBackupFolder\RemovableStorage.txt | Out-GridView -OutputMode Single -Title "Choose USB Storage Device" | Out-File $locBackupFolder\Drive.txt -Encoding default
$SelectedDrive = Get-Content "$locBackupFolder\Drive.txt"
You need to remove this block of code:
Out-File $locBackupFolder\Drive.txt -Encoding default
It is creating a new blank Drive.txt file, after which, Get-Content is being called on that empty file.
Therefore, it is getting the content of a blank file, which will always result in the $SelectedDrive variable being set to null.
I've added content to my Drive.txt file after this block has been removed and the $SelectedDrive variable is set to the content of the file as expected
This construct won't work as you seem to expect:
Get-Content file | Out-GridView | Out-File otherfile
Out-GridView doesn't pass the data back into the pipeline for further processing. If you need data in both a grid view and a file you need to use the Tee-Object cmdlet before Out-GridView:
Get-Content file | Tee-Object otherfile | Out-GridView
In your case:
Get-Content "$locBackupFolder\RemovableStorage.txt" |
Tee-Object "$locBackupFolder\Drive.txt" |
Out-GridView -OutputMode Single -Title 'Choose USB Storage Device'
If you want the user to select a particular Item, Out-GridView won't work at all, because it's for output only (hence the verb Out). You'll need a form with a list box to allow the user to select an item:
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$frm = New-Object Windows.Forms.Form
$btn = New-Object Windows.Forms.Button
$btn.Location = New-Object Drawing.Size(10,220)
$btn.Size = New-Object Drawing.Size(75,23)
$btn.Text = "OK"
$btn.Add_Click({$frm.Close()})
$frm.Controls.Add($btn)
$list = New-Object Windows.Forms.ListBox
$list.Location = New-Object Drawing.Size(10,10)
$list.Size = New-Object Drawing.Size(260,40)
$list.Height = 200
Get-Content "$locBackupFolder\RemovableStorage.txt" | % {
$list.Items.Add($_)
} | Out-Null
$frm.Controls.Add($list)
$frm.Add_Shown({$frm.Activate()})
[void]$frm.ShowDialog()
$SelectedDrive = $list.SelectedItem

How to check if SQL Server 2008 R2 database exists using power shell

I have a script for a database which works perfectly, however I ran into a problem where I need an if statement which checks if the database specified by user already exists. If it does, then the script should create a backup of it, if it does not exist, then the script should show an error.
Here is my script
#Loads Assembly
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-
Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") "(local)"
$bkdir = "C:\DBbackups" #We define the folder path as a variable
$database = Read-Host "Please Specify Database Name"
$dbs = $server.Databases
#To Backup one Database specify exact name
#To backup multiple database which starts with same name use "*"
foreach ($db in $dbs)
{
if($db.Name -like $database)
{
$dbname = $db.Name
$dt = get-date -format yyyyMMddHHmmss #We use this to create a file name based on the timestamp
$dbBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup")
$dbBackup.Action = "Database"
$dbBackup.Database = $dbname
$dbBackup.Devices.AddDevice($bkdir + "\" + $dbname + "_db_" + $dt + ".bak", "File")
$dbBackup.SqlBackup($server)
write-host "Database backup is successful for"$dbname
write-host "."
write-host "New Backup file is"$dbname"_db_"$dt".bak"
}
}
write-host "New Backup Location is" $bkdir
This is what i have modified to
foreach ($db in $dbs)
{
if($db.Name -like $database)
{
$dbname = $db.Name
$dt = get-date -format yyyyMMddHHmmss #We use this to create a file name based on the timestamp
$dbBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup")
$dbBackup.Action = "Database"
$dbBackup.Database = $dbname
$dbBackup.Devices.AddDevice($bkdir + "\" + $dbname + "_db_" + $dt + ".bak", "File")
$dbBackup.SqlBackup($server)
write-host "Database backup is successful for"$dbname `n
write-host "New Backup file is"$dbname"_db_"$dt".bak" `n
}
else {
write-host "invalid"
}
}
Try adding an else to the if ($db.Name -like $database) block if ($db.Name -like $database) { # do current backup stuff }
else { write-output "Database doesn't exist" }