tag:blogger.com,1999:blog-49685702154536559122024-03-23T12:17:26.598+02:00Use-PowershellUnknownnoreply@blogger.comBlogger162125tag:blogger.com,1999:blog-4968570215453655912.post-42180904444105573312023-08-09T14:13:00.008+03:002023-08-09T14:13:55.179+03:00List all hyper-v host in a scvmm cluster<p> Get-SCVMHost -VMHostCluster ( Get-SCVMHostCluster -Name cluster_name) | Select-Object Name</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-5488143229669272192023-07-25T18:31:00.007+03:002023-07-25T18:31:48.709+03:00Rescan disks remotely for a list of computers<p> Get-Content .\records.txt | %{Invoke-Command -ComputerName $PSItem -ScriptBlock {@('RESCAN','EXIT') | diskpart}}</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-3815125147037754542023-02-22T21:40:00.003+02:002023-02-22T21:40:25.109+02:00Define powershell custom objects<p> $hash = @{<br /> Prop1 = 'Value1'<br /><span> </span>Prop2 = 'Value2'<br /><span> </span>Prop3 = 'Value3'<br />}</p><p>$CustomObject = [pscustomobject]$hash</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-31496123217437149102023-02-22T15:55:00.007+02:002023-03-02T15:29:48.806+02:00Powershell generic List<p>list are allways to be used what the collection has to grow or shrink.</p><p>arrays should be used only when the collection have a fixed length and it will not change.</p><p><br /></p><p>[System.Collections.Generic.List[string]]::new()</p><p>[System.Collections.Generic.List[int]]::new()</p><p>[System.Collections.Generic.List[Object]]::new()</p><p><br /></p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-38506396604962763072023-02-22T02:02:00.006+02:002023-02-22T02:02:38.399+02:00Powershell - ignoring output performance<p> </p><p>(Measure-Command -Expression { [void]$(1..100000) }).Milliseconds</p><p>53</p><p>(Measure-Command -Expression { $null = $(1..100000) }).Milliseconds</p><p>49</p><p>(Measure-Command -Expression { $(1..100000) > $null }).Milliseconds</p><p>62</p><p>(Measure-Command -Expression { $(1..100000) | Out-Null }).Milliseconds</p><p>393</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-21672924330323007712022-10-12T15:25:00.000+03:002022-10-12T15:25:05.868+03:00Hyper-v - modify maximum simultaneous live migrations / storage migrations<p> </p><p>Get-SCVMHostCluster | Select-Object -ExpandProperty nodes | Select-Object name | % {Set-VMHost -ComputerName $PSItem.Name -MaximumStorageMigrations 2 -MaximumVirtualMachineMigrations 2 }</p><p><br /></p><p><br /></p><p>Get-SCVMHostCluster | Select-Object -ExpandProperty nodes | Select-Object name | % {Get-VMHost -C\omputerName $PSItem.Name } | Format-Table -AutoSize Name, MaximumStorageMigrations, MaximumVirtualMachineMigrations</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-12255272112792860782021-12-19T19:36:00.006+02:002021-12-19T19:36:36.484+02:00Select-Object splatting<p> $PropSplat = @(<br /><span> </span>'Name'<br /> 'Status'</p><p><span> </span>@{<br /> Name = 'BoolStatus'<br /> Expression = {<br /> if($PSItem.Status -eq 'Running'){$false}else{$true}<br /> }<br /> }<br />)</p><p>Get-Service m* | Select-Object $PropSplat</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-11202899756020040812021-12-17T16:30:00.002+02:002021-12-17T16:30:40.551+02:00Return line from CSV file (data)<p> in order to find and return a specific line from a csv file (data)</p><p><br /></p><p>$Recepies = Iomport-CSV C:\temp\recepies.scv</p><p>$Recepies.Where({$PSItem.Ingredient_1 -eq 'potato'})</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-33264632790788483672021-11-30T14:32:00.005+02:002021-11-30T14:32:24.969+02:00nslookup with arguments<p> in order to use nslookup with arguments</p><p><br /></p><p>nslookup -type=NS microsoft.com</p><p>nslookup -type=NS microsoft.com ns1-205.azure-dns.com</p><p><br /></p><p><br /></p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-76197524660055857632021-02-24T23:59:00.000+02:002021-02-24T23:59:01.898+02:00Search and install packages from powershellgallery<p>use this command to modify TLS settings if you get an error running the below commands<br />[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12</p><p>search for all available package providers<br />Find-PackageProvider</p><p>search for a specific package provider<br />Find-PackageProvider -name nuget</p><p>install a package provider<br />Install-PackageProvider -Name nuget</p><p><br /></p><p>after you have a package provider setup you can search for packages</p><p>search for a specific package. it supports wildcard in name<br />Find-Package -Name *AzureAD*</p><p>install package<br />Install-Package -Name AzureAD</p><div><br /></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-36077218826282783102021-02-24T23:47:00.001+02:002021-02-24T23:47:13.369+02:00Change domain for microsoft 365 / azure users from powershell<p><span style="font-size: 11pt; line-height: 115%;"><span style="font-family: Calibri, sans-serif;">modify TLS settings if you get errors running commands below<br /></span></span><span style="font-family: Calibri, sans-serif; font-size: 11pt;">[Net.ServicePointManager]::SecurityProtocol
= [Net.SecurityProtocolType]::Tls12</span></p><p><br /></p><p>Install-Module -Name MSOnline<br />(from https://www.powershellgallery.com )</p><p><span> </span><br /></p><p>Connect-MsolService</p><p>$AllMsolUsers = Get-MsolUser -All<br />$AllToModify = $AllMsolUsers | Where-Object -FilterScript { ($PSItem.Licenses.AccountSkuId -eq 'o365:STANDARDPACK') -and ($PSItem.UserType -eq 'Member') -and $PSItem.IsLicensed}</p><p>foreach($User in $AllToModify){</p><p> if($User.UserPrincipalName.Contains('@o365.onmicrosoft.com')){</p><p> $NewPrincipal = $User.UserPrincipalName.Replace('@o365.onmicrosoft.com','@domain.com')</p><p> Set-MsolUserPrincipalName -NewUserPrincipalName $NewPrincipal -UserPrincipalName $User.UserPrincipalName</p><p> }</p><p>}</p><div><br /></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-14922080418516369792021-02-23T16:35:00.004+02:002021-02-23T16:35:38.507+02:00WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2/'<p> </p><p>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-53514986646880290262021-02-21T13:31:00.006+02:002021-02-21T13:31:41.579+02:00Create hashtable from PSObject <p>$GroupDataObjectHash = [ordered]@{}</p><p>$GroupDataObject.PSObject.Properties.Name | %{$GroupDataObjectHash.Add($PSItem, $GroupDataObject.$PSItem)}</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-26074681329716047522019-03-19T09:57:00.002+02:002019-03-19T09:57:25.198+02:00Capture network traffic without installing 3th party tools<br />
1. in an elevated command prompt window run:<br />
<span style="font-family: Calibri, sans-serif; font-size: 11pt; font-weight: bold;">netsh trace start
persistent=yes capture=yes tracefile=c:\temp\tcpdump.etl</span><br />
<span style="font-family: Calibri, sans-serif; font-size: 11pt;">2. reproduce the issue (can also do a reboot of the computer - the traffic will be captured)</span><br />
<span style="font-family: Calibri, sans-serif; font-size: 11pt;">3. </span>in an elevated command prompt window run:<br />
<b>netsh trace stop</b><br />
<b><br /></b>
<br />
<div class="MsoNormal">
It will also capture while restarting (until netsh trace
stop)<o:p></o:p></div>
<b></b>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-88625841621556700472018-05-25T10:27:00.001+03:002018-05-25T10:27:14.752+03:00Find filesystem blocksize on windows with powershellGet-WmiObject -Class Win32_Volume | Select-Object Name, Label, BlockSize<br />
<br />
Get-CimInstance -ClassName Win32_Volume | Select-Object Name, Label, BlockSizeUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-40367211342547780662018-05-22T11:43:00.000+03:002018-05-22T11:43:29.568+03:00Find exception full name$Error[0] | Select-Object *<br />
<br />
Exception : Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException: The specified account already exists<br />
<br />
<br />
<br />
$Error[0].Exception.GetType().FullName<br />
Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsExceptionUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-74555721007024530342018-03-14T15:55:00.004+02:002018-03-14T15:55:37.380+02:00Exchange 2016 cmdlet helphelp is broken in Exchange 2016 PowerShell console - the only way to make it work i found to be this:<br />
<br />
<br />
<div class="MsoNormal">
$Session = New-PSSession -ConfigurationName
Microsoft.Exchange
-ConnectionUri http://exchangeservername/PowerShell/?SerializationLevel=None<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Invoke-Command –Session $Session –ScriptBlock {Get-Help
Get-Mailbox} <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
but you will not be abble to import the exchange cmdles through this pssession because it will throw an error.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
to import the cmdlest you need to have a different pssession without "SerializationLevel=None"</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
$Session2 = New-PSSession -ConfigurationName
Microsoft.Exchange
-ConnectionUri http://exton.office.orange.intra/PowerShell<o:p></o:p></div>
<div class="MsoNormal">
Import-PSSession $Session2<o:p></o:p></div>
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-37451910098342581292018-03-02T18:22:00.001+02:002018-03-02T18:22:58.530+02:00Get-WmiObject hangsfor some reason Get-WmiObject hangs on some of the computers that is queryes and the powershell console must be restarted.<br />
<br />
in order to overcome this problem a used "-AsJob" parameter<br />
<br />
<br />
$Job = Get-WmiObject -Class win32_computersystem -AsJob -ComputerName ServerName | Wait-Job -Timeout 30<br />
$Result = $Job | Receive-JobUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-84219129737831200102018-01-27T14:21:00.001+02:002018-01-27T14:21:20.251+02:00Protect OU (Organizational Unit) in AD from accidental deletionto verify if your OUs are protected from accidental deletion use:<br />
<br />
Get-ADOrganizationalUnit -Filter * -Properties * |Select-Object DistinguishedName, ProtectedFromAccidentalDeletion<br />
<br />
<br />
to protect all OUs in AD from accidental deletion use:<br />
<br />
Get-ADOrganizationalUnit -Filter * | Set-ADObject -ProtectedFromAccidentalDeletion:$true<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-72454329346398371892018-01-27T11:49:00.000+02:002018-01-27T11:49:18.645+02:00Find FSMO roles from powershellGet-ADDomainController -Filter * | Select-Object Name, Site, OperatingSystem, OperationMasterRolesUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-40204307300733563962018-01-21T19:13:00.001+02:002018-01-21T19:13:45.684+02:00Search eventlogs with xml filter# 4625 bad password in client log<br />
# 4771 bad password in DC log<br />
# 4740 lockout in DC log<br />
# <Select Path="Security">*[System[(EventID=4740 or EventID=4771)]]</Select><br />
<br />
[xml]$XMLFilter = @"<br />
<QueryList><br />
<Query Id="0" Path="Security"><br />
<Select Path="Security">*[System[(EventID=4740)]]</Select><br />
</Query><br />
</QueryList><br />
"@<br />
<br />
$AllDomainControllers = Get-ADDomainController -Filter *<br />
<br />
$AllEvents = @()<br />
<br />
foreach($DC in $AllDomainControllers){<br />
$Events = @()<br />
$Events += Get-WinEvent -FilterXml $XMLFilter -ComputerName $DC.HostName -ErrorAction SilentlyContinue<br />
$AllEvents += $Events<br />
$DC.HostName + ' ' + $Events.Length<br />
}<br />
<br />
foreach($Event in $AllEvents){<br />
$EventXMLData = [xml]$Event.ToXml()<br />
for($i=0; $i -lt $EventXMLData.Event.EventData.Data.Count; $i++){<br />
$Name = $EventXMLData.Event.EventData.Data[$i].Name<br />
$Value = $EventXMLData.Event.EventData.Data[$i].'#text'<br />
Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name $Name -Value $Value<br />
}<br />
}<br />
<br />
$AllEvents |<br />
Select-Object TargetUsername, MachineName, TimeCreated,IpAddress, ID |<br />
Format-Table<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLew9CmwcH0AC6C5D1-5BFtSaxw0UwM6HGj76uBvIDt6RwAMFCw2QpN7Az31TdS6gRWv0QmsPA2MexFrD9FR87EameBMbOgkDHhje8sjE8WLRfoBf5rcPk7RtEXzLZokgT9HNAO933/s1600/filtereventlog.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="553" data-original-width="547" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLew9CmwcH0AC6C5D1-5BFtSaxw0UwM6HGj76uBvIDt6RwAMFCw2QpN7Az31TdS6gRWv0QmsPA2MexFrD9FR87EameBMbOgkDHhje8sjE8WLRfoBf5rcPk7RtEXzLZokgT9HNAO933/s320/filtereventlog.JPG" width="316" /></a></div>
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-70225629625872944062018-01-21T18:21:00.002+02:002018-01-21T18:24:43.539+02:00Active directory domain user last logon date and time<br />
"lastLogon" attribute is per domain controller - is not replicated to other domain controllers in the domain and each domain cotroller has his own information.<br />
<br />
"lastLogonTimeStamp" is replicated in the domain (all domain controllers have the same updated information).<br />
<br />
the date is stored in 100 miliseconds interval since 01.01.1601 (Juanuary 1, 1601)<br />
<br />
to convert from System.Int64 we can use "FromFileTime" static method of DateTime class:<br />
<br />
[System.DateTime]::FromFileTime($ADUser.lastlogon)<br />
[System.DateTime]::FromFileTime($ADUser.lastlogontimestamp)<br />
<br />
http://msdn.microsoft.com/en-us/library/ms676824(VS.85).aspx<br />
http://msdn.microsoft.com/en-us/library/ms676823(VS.85).aspx<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgN-Go-mNgerpQwOH7GiGvx9z0_hbLewPFGbcmVjH8jXnHXo78cyJ34c_7wWCw5_d0JlwJj9YWampwel0NRnfzqgzYAty6BTEz77pjOzblKH-GDZEFAERDgLnW_pDp_i102ZDzXnAvc/s1600/lastlogon.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="467" data-original-width="423" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgN-Go-mNgerpQwOH7GiGvx9z0_hbLewPFGbcmVjH8jXnHXo78cyJ34c_7wWCw5_d0JlwJj9YWampwel0NRnfzqgzYAty6BTEz77pjOzblKH-GDZEFAERDgLnW_pDp_i102ZDzXnAvc/s320/lastlogon.JPG" width="289" /></a></div>
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-24011974803017802232018-01-21T17:00:00.003+02:002018-01-21T17:00:59.731+02:00Active Directory Technical Specification<div>
<br /></div>
<div>
search the web for "MS-ADTS"</div>
<div>
<br /></div>
<div>
https://msdn.microsoft.com/en-us/library/cc223122.aspx</div>
<div>
<br /></div>
<div>
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-90447425164784530022018-01-21T13:27:00.000+02:002018-01-21T14:12:47.002+02:00List active directory group membership changes<br />
$ADGroup = Get-ADGroup -Identity 'Domain Admins'<br />
Get-ADReplicationAttributeMetadata -Object $ADGroup.DistinguishedName -Server dc1 -ShowAllLinkedValues<br />
<br />
in the output the "AttributeName" is the attribute that was changed - we should search for the "member" attribute.<br />
<br />
"AttributeValue" is the value assigned to the attribute.<br />
"FirstOriginatingCreateTime" is the time the value was added.<br />
"LastOriginatingDeleteTime" is the time the value was deleted - but only if is different from "1/1/1601 2:00:00 AM"Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4968570215453655912.post-74876944177807684912017-11-13T12:32:00.001+02:002017-11-13T12:32:23.674+02:00Ldap filter to recursively find all groups a user is member<br />
Get-ADGroup -LDAPFilter "(member:1.2.840.113556.1.4.1941:=CN=Uservane,OU=Users,DC=domain,DC=local)"Unknownnoreply@blogger.com0