29 December 2016

Dispose COM objects as it can cause memory leaks

# dispose COM objects as it can cause memory leaks
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($VariableName) | Out-Null

15 December 2016

Calculate MD5 with powershell


[Reflection.Assembly]::LoadWithPartialName("System.Web")
[System.Web.Security.FormsAuthentication]::HashPasswordForStoringInConfigFile("p@ssw0rd", "MD5")

08 December 2016

Display a MessageBox from PowerShell


[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Windows.Forms.MessageBox]::Show("Hello there !")
[System.Windows.Forms.MessageBox]::Show("Hello there !", "Some title")


you can also have buttons like:

0: OK
1: OK Cancel
2: Abort Retry Ignore
3: Yes No Cancel
4: Yes No
5: Retry Cancel

[System.Windows.Forms.MessageBox]::Show("Hello there !", "Some title", 4)



you can olso read the unswer from the user:

$Unswer = [System.Windows.Forms.MessageBox]::Show("Hello there !", "Some title", 4)
if ($Unswer -eq "YES" ) { # perform sone task }
else { # perform some other task}

more info on msdn

20 October 2016

Grant sharing permissions for multiple users with net share

net share sharename=path_to_folder /GRANT:username,FULL /GRANT:username2,FULL

18 October 2016

Get computer manufacturer and model

command prompt
wmic computersystem get model, manufacturer


powershell
Get-WmiObject Win32_ComputerSystem | Select-Object Manufacturer, Model


visual

msinfo32.exe

17 October 2016

Retrieve HBA information with powershell

$Params = @{
        Namespace = 'root\WMI'
        class = 'MSFC_FCAdapterHBAAttributes'
        ComputerName = $Computer
        ErrorAction = 'Stop'
}
 
$HBAAdapters = Get-WmiObject @Params

foreach($HBA in $HBAAdapters){
    $HBAInfo=@{
            ComputerName = $HBA.__SERVER
            NodeWWN = (($HBA.NodeWWN) | ForEach-Object {"{0:X2}" -f $_}) -join ":"
            Active = $HBA.Active
            DriverName = $HBA.DriverName
            DriverVersion = $HBA.DriverVersion
            FirmwareVersion = $HBA.FirmwareVersion
            Model = $HBA.Model
            ModelDescription = $HBA.ModelDescription
}
New-Object psobject -Property $HBAInfo

source

13 October 2016

Find users with active sync enabled

Get-CASMailbox -ResultSize Unlimited | Where-Object { $_.ActiveSyncEnabled -eq 'True'} | Select-Object name, activesyncenabled

12 October 2016

Generate file of speciefic size

param(
    [string]$FileName,
    [double]$FileSize
)
    $FilePath = ((Get-Location).Path) + '\' + $FileName
    $File = [System.IO.File]::Create($FilePath)
    $File.SetLength($FileSize)
    $File.Close()

    Get-Item $File.Name

15 March 2016

Execute stored procedure in oracle database from powershell



 $connectionString =“User Id=username;Password=password;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=appname.domain.intra)(PORT=1523)))(CONNECT_DATA=(SID=sid)))”;

$oracleConnection = new-object System.Data.OracleClient.OracleConnection($connectionString);
$ProcName = "procedure_name";
$oracleConnection.Open();
$cmd = new-object System.Data.OracleClient.OracleCommand;
$cmd.Connection = $oracleConnection;
$cmd.CommandText = $ProcName;
$cmd.CommandType = [System.Data.CommandType]::StoredProcedure;
$cmd.ExecuteNonQuery();
$oracleConnection.Close();

04 February 2016

Ldap filter for deleted objects


to get all deleted computer accounts:


Get-ADObject -LDAPFilter "(&(ObjectClass=computer)(isDeleted=*))" -IncludeDeletedObjects -Server servername

19 January 2016

Ldap filter enabled users


(&(objectCategory=organizationalPerson)(objectClass=User)(userAccountControl:1.2.840.113556.1.4.803:=2))


for disabled users:

(&(objectCategory=organizationalPerson)(objectClass=User)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))

09 January 2016

Windows buit-in checksum utility


certutil -hashfile pathToFileToCheck MD5

it can also calculate for MD2 MD4 MD5 SHA1 SHA256 SHA384 SHA512

08 January 2016

Graphical User Interface for PowerShell scripts

We have two options for creating a GUI for a PowerShell script:
  • Windows Forms (WinForms)
  • Windows Presentation Foundation (WPF)
WinForms use only native Windows interface elements - limited control on how things will look like.

With WPF you have full freedom of design but it's way more complicated than WinForms.
WPF is built on DirectX - this provide hardware acceleration and also enables modern UI features like transparency, gradients and transforms.

Also WPF offers a new markup language alternative - XAML - a different means of defining UI elements. An application that is defined as WPF is able to be deployed on the desktop or hosted on a webserver.

WinForms are events driven.

07 January 2016

Convert exported FIM object to a custom PSObject

Function Convert-FimObjectToPSObject {
    Param (
        [parameter(Mandatory=$true, ValueFromPipeline = $true)]
        [Microsoft.ResourceManagement.Automation.ObjectModel.ExportObject]
        $FIMObject
    )
    Process
    {      
        $PSObject = New-Object PSObject

        foreach($Attribute in $FIMObject.ResourceManagementObject.ResourceManagementAttributes){
                if ($Attribute.Value -ne $null) { $Value = $Attribute.Value }
                elseif($Attribute.Values -ne $null)  { $Value = $Attribute.Values  }
                else  { $Value = $null }

                $PSObject | Add-Member -MemberType NoteProperty -Name $Attribute.AttributeName -Value $Value
        }
        Write-Output $PSObject
    }
}

$FIMPSObject = Convert-FimObjectToPSObject -FIMObject $FimGroup
$FIMPSObject