Thursday, February 16, 2017

get network share and grant full access using powershell Smb-commands


I had problem when I created network share using command Get-WmiObject Win32_Share I only had read access. So I found out that granting full access can be done by Grant-SmbShareAccess.


To get smb share:
Get-SmbShare -Name shareName

To grant user access:
Grant-SmbShareAccess -Name shareName-AccountName accountName@domain.local -AccessRight Full

Monday, January 2, 2017

Backup to network folder, display message if folder not available. [Powershell]

This example backups Thunderbird profiles to precreated network share. It can be added to task scheduler. It sends net message to current user if network share does not exist.

$name="user"
$backupDir = "\\server\backup\workstations"
$backupFolder = $backupDir + "\" + $name + "\"
$date = get-date -Format ddd



echo $backupFolder
echo $env:APPDATA/Thunderbird
cd $env:APPDATA/Thunderbird


if (-Not (Test-Path -Path $backupFolder)) { 
msg.exe $env:UserName "Path[$backupFolder] not found, unable to backup"
return 0x10
}



if (-Not (Test-Path -Path $backupFolder$date)) {
New-Item -Path "$backupFolder$date" -ItemType Directory
}
else
{
echo "Path: $backupFolder$date already exists, skipping"
}



Copy-Item "Profiles/*" -Recurse -Destination $backupFolder$date -Force

Tuesday, May 12, 2015

How To - Monitor virtual machines and send email if something is not right - Powershell V4.0

Today I wanted to make automated monitor script which monitors Virtual Machine states and alerts me if something is not normal. Here is what I came up with:

$vms = Get-VM -ComputerName s2012-hv01,s2012-hv04,HV02
$warningMessage='';
foreach ($vm in $vms)
{
    if ($vm.Status -ne "Operating normally")
    {
        $warningMessage += "`n$($vm.name) [$($vm.ReplicationMode)] is not operating normally on $($vm.ComputerName), status: $($vm.status)! "
    }
    elseif ($vm.ReplicationState -ne "Replicating")
    {
        $warningMessage += "`n$($vm.name) [$($vm.ReplicationMode)] has invalid replication state($($vm.ReplicationState)) on $($vm.ComputerName)! "
    }
    elseif($vm.MemoryStatus -ne 'OK' -and $vm.MemoryStatus -ne '')
    {
        $warningMessage += "`n$($vm.name) [$($vm.ReplicationMode)] has invalid memory status($($vm.MemoryStatus)) on $($vm.ComputerName)! "
    }
    elseif($vm.ReplicationHealth -ne "Normal")
    {
        $warningMessage += "`n$($vm.name) [$($vm.ReplicationMode)] does not have healthy replication status($($vm.ReplicationHealth)) on $($vm.ComputerName)! "
    }
}
if ($warningMessage -ne '')
{
    #[System.Windows.Forms.MessageBox]::Show("Replikoinnissa on virhe. $warningMessage","Virtual machine issue!")
    Send-MailMessage -To "ex.ample@example.fi" -From "monitor@example.fi" -Body "Virtual machine issue`n$warningMessage`n`n" -Subject "Virtual machine issue" -SmtpServer "smtp.example.fi" -Encoding UTF8
}
Code will check some states and send an email if something is not right. I added this to task scheduler and run it as administrator (user which is not logged on normally) on startup + repeat every 30mins. Commented line is for messagebox but I was not able to use it on task scheduler, thus mail.