$admin = [adsi]("WinNT://$ComputerName/$AccountName, user") $admin.psbase.invoke("SetPassword", $Password)
While I was looking for that command, I remembered this other ways to do the same thing.
net user $account $password
Some problems you just can't search on. Here are some I wish were more searchable and this blog is my attempt to make that happen.
$admin = [adsi]("WinNT://$ComputerName/$AccountName, user") $admin.psbase.invoke("SetPassword", $Password)
net user $account $password
#AccountManagement is used for Varifying password changes Add-Type -AssemblyName System.DirectoryServices.AccountManagement function Set-AccountPassword { <# .Synopsis Sets the password for a local machine account .DESCRIPTION It will set a password on a remote machine for the specified account with the specified password .EXAMPLE Set-AccountPassword -ComputerName localhost -AccountName Administrator -Password BatteryStapleHorse .EXAMPLE Set-AccountPassword -Password BatteryStapleHorse -SkipVerify -Force #> [CmdletBinding(DefaultParameterSetName='Default', SupportsShouldProcess, PositionalBinding=$false, HelpUri = 'http://www.microsoft.com/', ConfirmImpact='Medium')] Param ( # ComputerName help description [Parameter(Mandatory=$false, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [string]$ComputerName = "$env:computername", # AccountName help description [Parameter(Position=1)] [string] $AccountName = 'Administrator', # Password help description [Parameter(Position=2, Mandatory=$true, ParameterSetName="Default")] [ValidateLength(15,139)] [String] $Password, [Parameter()] [switch]$SkipVerify, [Parameter()] [switch]$Force ) Begin { } Process { Write-Verbose "Testing connection to $ComputerName before we try and change the password" if(Test-Connection $ComputerName -Count 1){ Write-Verbose "$ComputerName is online" #Example of support for -WhatIf #Also used with -Confirm and ConfirmImpact options if ($pscmdlet.ShouldProcess("$ComputerName\$AccountName", "SetPassword")) { #Example of support for -Force, this will prompt every time unless the -force param is used if($Force -or $pscmdlet.ShouldContinue("Change the password for this account: $ComputerName\$AccountName","Setting Password")){ Write-Verbose "Using ADSI for connection to WinNT://$ComputerName/$AccountName" $admin = [adsi]("WinNT://$ComputerName/$AccountName, user") Write-Verbose "Invoking SetPassword on $ComputerName" $admin.psbase.invoke("SetPassword", $Password) # This will verify that the password was changed to $Password # Skip Verify is an optional param if(!$SkipVerify){ Write-Verbose "Verifing that the password changed correctly" $obj = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('machine',"$ComputerName") if($obj.ValidateCredentials("$AccountName","$Password")){ Write-Verbose "Verified!" }else{ Write-Error "Failed to verify password change" } }else{ Write-Verbose "SkipVerify=True skipping verify check" } #SkipVerify } #ShouldContinue } #ShouldProcess } #Test-Connection } #Process End { } }
$ConfigData = @{ AllNodes = @( @{ NodeName = "*" }, @{ NodeName = "ProdServer1"; Database = "database"; DatabaseServer = "DBServer" }, @{ NodeName = "ProdServer2"; Database = "database" DatabaseServer = "DBServer" }, @{ NodeName = "QAServer1"; Database = "QAdatabase" DatabaseServer = "DBServer" } ) } Configuration TerminalServer { node $allnodes.NodeName { WindowsFeature Backup { Name = "Windows-Server-Backup"; } } node $allnodes.NodeName { WindowsFeature DesktopExperience { Name = "Desktop-Experience"; IncludeAllSubFeature = "True"; } WindowsFeature RDS { Name = "RDS-RD-Server"; IncludeAllSubFeature = "True"; } Registry ProductdDatabase{ Key = "HKLM:\SOFTWARE\company\product"; ValueName = 'InitialCatalog'; ValueData = $node.Database } Registry ProductDatabaseServer{ Key = "HKLM:\SOFTWARE\company\product"; ValueName = 'DataSource'; ValueData = $node.DatabaseServer } } } TerminalServer -ConfigurationData $ConfigData Start-DscConfiguration -Wait -Verbose -Path .\TerminalServer
$EventLog = Get-WinEvent -ListLog Microsoft-Windows-PrintService/Operational $EventLog | %{$_.IsEnabled = $true; $_.MaximumSizeInBytes=50MB; $_.LogMode = "AutoBackup" $_.SaveChanges()}
$log = Get-WinEvent -FilterHashTable @{ "LogName"= "Microsoft-Windows-PrintService/Operational";"ID"="307"}
$log | ?{$_.message -match $MessageRegEx} | %{ New-Object PSObject -property @{"Document"=$Matches.Document; "UserName"=$Matches.Username; "IP"=$Matches.IP; "ComputerName"=$Matches.Computer; "Pages"=$Matches.Pages; "TimeStamp"= $_.TimeCreated; "Printer" = $Matches.Printer; "PrintHost" = $_.MachineName }}