Sunday, December 22, 2019

upgrading powershellget module

Start-Process powershell.exe -ArgumentList "-command `"& {Install-PackageProvider Nuget –Force}`""
Start-Process powershell.exe -ArgumentList "-command `"& {Install-Module –Name PowerShellGet –Force}`""
Start-Process powershell.exe -ArgumentList "-command `"& {Install-Module –Name PowerShellGet –Force}`""

Update-Module -Name PowerShellGet

get-module



get-psrepository -Name PSGallery | Set-PSRepository -InstallationPolicy Trusted
find-module universaldashboard | Install-Module -AcceptLicense
choco install dotnet4.7.2 -y

Wednesday, November 27, 2019

Send message in RDS to logoff

Import-Module RemoteDesktop
Import-Module RemoteDesktopServices

$primaryBroker = (Get-RDConnectionBrokerHighAvailability -ConnectionBroker Servername).ActiveManagementServer
$c = get-RDSessionCollection -ConnectionBroker $primaryBroker

$c| % {
    $coll = $_.CollectionName

    write-host "Active users in $($_.CollectionName)" -ForegroundColor Cyan
    Get-RDUserSession -ConnectionBroker $primaryBroker -CollectionName $_.collectionName | ? sessionstate -EQ "State_active" |% {

    Write-Host $_.username
    Send-RDUserMessage -HostServer $_.HostServer -UnifiedSessionID $_.UnifiedSessionId  -MessageTitle "Message from Administrator" -MessageBody "Please save your work. You will be logged off in 5 minutes"

    }
}

Monday, November 18, 2019

Change accelerated network settings

$nic = Get-AzNetworkInterface -Name netname -ResourceGroupName resourcegroup
$nic.EnableAcceleratedNetworking = $false
$nic| Set-AzNetworkInterface

Saturday, November 16, 2019

Change storage account Azure tier to archive

$resourcegroup="archiwum"
$StorageAccount="archiwumzdjecsa"
$container="zdjecia"
$stgkey=((Get-AzStorageAccountKey -ResourceGroupName $resourcegroup -Name $StorageAccount)[0]).Value
$ctx =New-AzureStorageContext -StorageAccountName $StorageAccount -StorageAccountkey $stgkey
$blob= Get-AzureStorageBlob -Container $container -Context $ctx |  where AccessTier -ne "Archive"
$blob.ICloudBlob.SetStandardBlobTier("Archive")

Thursday, November 7, 2019

find out event log about locked out account


Use Account Lockout Status (LockoutStatus.exe) first

Invoke-Command ServerName{(Get-WinEvent -FilterHashtable @{logname="Security";id=4740} -MaxEvents 3).message}

Get-WinEvent -FilterHashtable @{logname="Application";ProviderName="SQLSERVERAGENT";id=102} -MaxEvents 100

Thursday, October 31, 2019

Nice Powershell prompt

Here is a nice Powershell prompt

$str= @'
Set-Item -Path function:prompt -Value {
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
$color=if ((New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) {"Red"} else {"Cyan"}

$promptString = 'PS '+ (Get-History).Id[-1] + " "+ $(Get-Date -Format t) + " " + $(Get-Location) + '> '
Write-Host $promptString -NoNewline -ForegroundColor $color
return " "

}
'@

Set-Content $PSHome\Profile.ps1 -Value $str                           

Output:

PS 36 09:33 C:\>

Other option:
https://raw.githubusercontent.com/thomasrayner/dev-workstation/master/prompt.ps1

Install any font from https://github.com/ryanoasis/nerd-fonts and use it in conemu.

Tuesday, January 15, 2019

Managing windows time

Open an Administrator Command Prompt
tzutil /s "Central European Standard Time"
This config will set NT5DS configuration in ws32 config, meaning update time from domain hierarchy.
w32tm /config /syncfromflags:domhier /update /reliable:no
Check your peers afterwards
w32tm /query /peers

If you want to manually set time run it:
w32tm /config /manualpeerlist:ntp1.corpaddress.tdl /reliable:yes /syncfromflags:manual /update

w32tm /resync /rediscover
net stop w32time && net start w32time
Check it with W32tm /query /configuration

Syncing manually:

w32tm /stripchart /computer:ntp1.corpaddress.tdl
If you sync to Linux machine
/manualpeerlist:ntp1.corpaddress.tdl,0x1
Restore all settings:

net stop w32time
w32tm /unregister
w32tm /register
net start w32time

a

Monday, January 7, 2019

powershell commands

Creating new PS job:


Register-ScheduledJob -Name InstallXPS -ScriptBlock {Install-WindowsFeature -Name XPS-Viewer} -Trigger (New-JobTrigger -Once -At 05:20pm)

Register-ScheduledJob -Name ArchiveOLDfolders -ScriptBlock {compress-archive -Path F:\F00\MARKET\OLD\pol_proj\*,F:\F00\MARKET\OLD\pol_org\*,F:\F00\MARKET\OLD\Pol_Data\*,F:\F00\MARKET\OLD\Pol_Common\*,F:\F00\MARKET\OLD\Pol_Appl\* -DestinationPath F:\F00\MARKET\OLD\archive.zip} -Trigger (New-JobTrigger -Once -At 09:25am)

ipmo PSScheduledJob
$T = New-ScheduledTaskTrigger -Once -At 5:01PM
$action = New-ScheduledTaskAction -Execute 'C:\temp\compress.cmd'
$User= "NT AUTHORITY\SYSTEM"
Register-ScheduledTask -TaskName  ArchiveOldFiles -Action $action -User $User -RunLevel Highest –Force -Trigger $t


C:\tools\7-ZipPortable\App\7-Zip64\7z.exe a -r F:\F00\MARKET\OLD\pol_org.7z F:\F00\MARKET\OLD\Pol_org\* > C:\temp\pol_org.log
C:\tools\7-ZipPortable\App\7-Zip64\7z.exe a -r F:\F00\MARKET\OLD\pol_proj.7z F:\F00\MARKET\OLD\Pol_proj\* > C:\temp\pol_proj.log
C:\tools\7-ZipPortable\App\7-Zip64\7z.exe a -r F:\F00\MARKET\OLD\pol_data.7z F:\F00\MARKET\OLD\Pol_data\* > C:\temp\pol_data.log


Getting info when system was restarted

get-eventlog system | where-object {$_.eventid -eq 6006} | select -first 10

$out=Invoke-Command -ComputerName 'SFRFIDCSHP001t','SFRFIDCSHP004T','SFRFIDCSHP005T','SFRFIDCSHP006T' -ScriptBlock {get-eventlog system | where-object {$_.eventid -eq 6006} | select -first 5} 


$out | sort TimeGenerated -Descending | Export-Csv -Path c:\temp\sharepoint_restart2.csv