09 August 2023

List all hyper-v host in a scvmm cluster

 Get-SCVMHost -VMHostCluster ( Get-SCVMHostCluster -Name cluster_name) |  Select-Object Name

25 July 2023

Rescan disks remotely for a list of computers

 Get-Content .\records.txt | %{Invoke-Command -ComputerName $PSItem -ScriptBlock {@('RESCAN','EXIT') | diskpart}}

22 February 2023

Define powershell custom objects

 $hash = @{
    Prop1      = 'Value1'
    Prop2      = 'Value2'
    Prop3      = 'Value3'
}

$CustomObject = [pscustomobject]$hash

Powershell generic List

list are allways to be used what the collection has to grow or shrink.

arrays should be used only when the collection have a fixed length and it will not change.


[System.Collections.Generic.List[string]]::new()

[System.Collections.Generic.List[int]]::new()

[System.Collections.Generic.List[Object]]::new()


Powershell - ignoring output performance

 

(Measure-Command -Expression { [void]$(1..100000) }).Milliseconds

53

(Measure-Command -Expression { $null = $(1..100000) }).Milliseconds

49

(Measure-Command -Expression { $(1..100000) > $null }).Milliseconds

62

(Measure-Command -Expression { $(1..100000) | Out-Null }).Milliseconds

393

12 October 2022

Hyper-v - modify maximum simultaneous live migrations / storage migrations

 

Get-SCVMHostCluster | Select-Object -ExpandProperty nodes | Select-Object name | % {Set-VMHost -ComputerName $PSItem.Name -MaximumStorageMigrations 2 -MaximumVirtualMachineMigrations 2 }



Get-SCVMHostCluster | Select-Object -ExpandProperty nodes | Select-Object name | % {Get-VMHost -C\omputerName $PSItem.Name } | Format-Table -AutoSize Name, MaximumStorageMigrations, MaximumVirtualMachineMigrations

19 December 2021

Select-Object splatting

 $PropSplat = @(
    'Name'
    'Status'

    @{
        Name = 'BoolStatus'
        Expression = {
            if($PSItem.Status -eq 'Running'){$false}else{$true}
        }
    }
)

Get-Service m* | Select-Object $PropSplat

17 December 2021

Return line from CSV file (data)

 in order to find and return a specific line from a csv file (data)


$Recepies = Iomport-CSV C:\temp\recepies.scv

$Recepies.Where({$PSItem.Ingredient_1 -eq 'potato'})

30 November 2021

nslookup with arguments

 in order to use nslookup with arguments


nslookup -type=NS microsoft.com

nslookup -type=NS microsoft.com ns1-205.azure-dns.com



24 February 2021

Search and install packages from powershellgallery

use this command to modify TLS settings if you get an error running the below commands
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

search for all available package providers
Find-PackageProvider

search for a specific package provider
Find-PackageProvider -name nuget

install a package provider
Install-PackageProvider -Name nuget


after you have a package provider setup you can search for packages

search for a specific package. it supports wildcard in name
Find-Package -Name *AzureAD*

install package
Install-Package -Name AzureAD


Change domain for microsoft 365 / azure users from powershell

modify TLS settings if you get errors running commands below
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12


Install-Module -Name MSOnline
(from https://www.powershellgallery.com )

 

Connect-MsolService

$AllMsolUsers = Get-MsolUser -All
$AllToModify = $AllMsolUsers | Where-Object -FilterScript { ($PSItem.Licenses.AccountSkuId -eq 'o365:STANDARDPACK') -and ($PSItem.UserType -eq 'Member') -and $PSItem.IsLicensed}

foreach($User in $AllToModify){

    if($User.UserPrincipalName.Contains('@o365.onmicrosoft.com')){

        $NewPrincipal = $User.UserPrincipalName.Replace('@o365.onmicrosoft.com','@domain.com')

        Set-MsolUserPrincipalName -NewUserPrincipalName $NewPrincipal -UserPrincipalName $User.UserPrincipalName

    }

}


23 February 2021

21 February 2021

Create hashtable from PSObject

$GroupDataObjectHash = [ordered]@{}

$GroupDataObject.PSObject.Properties.Name | %{$GroupDataObjectHash.Add($PSItem, $GroupDataObject.$PSItem)}

19 March 2019

Capture network traffic without installing 3th party tools


1. in an elevated command prompt window run:
    netsh trace start persistent=yes capture=yes tracefile=c:\temp\tcpdump.etl
2. reproduce the issue (can also do a reboot of the computer - the traffic will be captured)
3.  in an elevated command prompt window run:
    netsh trace stop


It will also capture while restarting (until netsh trace stop)

25 May 2018

Find filesystem blocksize on windows with powershell

Get-WmiObject -Class Win32_Volume | Select-Object Name, Label, BlockSize

Get-CimInstance -ClassName Win32_Volume | Select-Object Name, Label, BlockSize

22 May 2018

Find exception full name

$Error[0] | Select-Object *

Exception  : Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException: The specified account already exists



$Error[0].Exception.GetType().FullName
Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException

14 March 2018

Exchange 2016 cmdlet help

help is broken in Exchange 2016 PowerShell console - the only way to make it work i found to be this:


$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchangeservername/PowerShell/?SerializationLevel=None

Invoke-Command –Session $Session –ScriptBlock {Get-Help Get-Mailbox}

but you will not be abble to import the exchange cmdles through this pssession because it will throw an error.

to import the cmdlest you need to have a different pssession without "SerializationLevel=None"


$Session2 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exton.office.orange.intra/PowerShell
Import-PSSession $Session2

02 March 2018

Get-WmiObject hangs

for some reason Get-WmiObject hangs on some of the computers that is queryes and the powershell console must be restarted.

in order to overcome this problem a used "-AsJob" parameter


$Job = Get-WmiObject -Class win32_computersystem -AsJob -ComputerName ServerName | Wait-Job -Timeout 30
$Result = $Job | Receive-Job

27 January 2018

Protect OU (Organizational Unit) in AD from accidental deletion

to verify if your OUs are protected from accidental deletion use:

Get-ADOrganizationalUnit -Filter * -Properties * |Select-Object DistinguishedName, ProtectedFromAccidentalDeletion


to protect all OUs in AD from accidental deletion use:

Get-ADOrganizationalUnit -Filter * | Set-ADObject -ProtectedFromAccidentalDeletion:$true

Find FSMO roles from powershell

Get-ADDomainController -Filter * | Select-Object Name, Site,  OperatingSystem, OperationMasterRoles