PS C:\Users\Administrator\Documents> Find-Module Posh-SSH Version Name Type Repository Description ------- ---- ---- ---------- ----------- 1.7.7 Posh-SSH Module PSGallery Provide SSH functionality for executi... PS C:\Users\Administrator\Documents> Install-Module Posh-SSH Untrusted repository You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolic y value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'? [Y] Yes [A] Yes to All [N] No [L] No to All [?] Help (default is 'Y'): PS C:\Users\Administrator\Documents> New-SSHSession -ComputerName "172.18.3.20" -Credential (Get-Credential) Windows PowerShell Credential Request : cmdlet Get-Credential at command pipeline position 1 Warning: This credential is being requested by a script or application on the STATBSPAPP remote computer. Enter your cr edentials only if you trust the remote computer and the application or script requesting it. Supply values for the following parameters: Credential: Enter your user name: root Enter your password: ***************** Server SSH Fingerprint Do you want to trust the fingerprint d4:d0:f9:cd:ad:22:ee:da:25:b2:2b:50:45:81:de:5a [] Y [] N [?] Help (default is 'N'): Y SessionId Host Connected --------- ---- --------- 0 172.18.3.20 True PS C:\Users\Administrator\Documents> Invoke-SSHCommand -Index 0 -Command "uname" Host : 172.18.3.20 Output : {SunOS} ExitStatus : 0 PS C:\Users\Administrator\Documents> Invoke-SSHCommand -Index 0 -Command "su - oracle -c id -u -n" Host : 172.18.3.20 Output : {uid=901(oracle) gid=900(dba)} ExitStatus : 0
Showing posts with label Windows PowerShell. Show all posts
Showing posts with label Windows PowerShell. Show all posts
Tuesday, January 24, 2017
Execute SSH on PowerShell
Wednesday, January 18, 2017
Windows Remote Management and PowerShell Web Access
PS C:\Users\Administrator> dir WSMan:\localhost\Listener\*\Port WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Listener\Listener_1084132640 Type Name SourceOfValue Value ---- ---- ------------- ----- System.String Port 5985 PS C:\Users\Administrator> Set-Item WSMan:\localhost\Listener\*\Port 8888 Set the value of the item This command set the value of the Item. Do you want to continue? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): PS C:\Users\Administrator> dir WSMan:\localhost\Listener\*\Port WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Listener\Listener_1892757853 Type Name SourceOfValue Value ---- ---- ------------- ----- System.String Port 8888 PS C:\Users\Administrator> PS C:\Users\Administrator> Enable-PSRemoting WinRM Quick Configuration Running command "Set-WSManQuickConfig" to enable remote management of this computer by using the Windows Remote Management (WinRM) service. This includes: 1. Starting or restarting (if already started) the WinRM service 2. Setting the WinRM service startup type to Automatic 3. Creating a listener to accept requests on any IP address 4. Enabling Windows Firewall inbound rule exceptions for WS-Management traffic (for http only). Do you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): WinRM is already set up to receive requests on this computer. WinRM has been updated for remote management. Configured LocalAccountTokenFilterPolicy to grant administrative rights remotely to local users. Confirm Are you sure you want to perform this action? Performing the operation "Set-PSSessionConfiguration" on target "Name: microsoft.powershell SDDL: O:NSG:BAD:P(A;;GA;;;BA)(A;;GA;;;RM)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD). This lets selected users remotely run Windows PowerShell commands on this computer.". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Confirm Are you sure you want to perform this action? Performing the operation "Set-PSSessionConfiguration" on target "Name: microsoft.powershell.workflow SDDL: O:NSG:BAD:P(A;;GA;;;BA)(A;;GA;;;RM)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD). This lets selected users remotely run Windows PowerShell commands on this computer.". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Confirm Are you sure you want to perform this action? Performing the operation "Set-PSSessionConfiguration" on target "Name: microsoft.powershell32 SDDL: O:NSG:BAD:P(A;;GA;;;BA)(A;;GA;;;RM)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD). This lets selected users remotely run Windows PowerShell commands on this computer.". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Confirm Are you sure you want to perform this action? Performing the operation "Set-PSSessionConfiguration" on target "Name: microsoft.windows.servermanagerworkflows SDDL: O:NSG:BAD:P(A;;GA;;;BA)(A;;GA;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD). This lets selected users remotely run Windows PowerShell commands on this computer.". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): PS C:\Users\Administrator> dir WSMan:\localhost\Listener\*\Port WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Listener\Listener_1892757853 Type Name SourceOfValue Value ---- ---- ------------- ----- System.String Port 8888
Check WinRM running with new ports
PS C:\Users\Administrator> netstat -aon | findstr "8888" TCP 0.0.0.0:8888 0.0.0.0:0 LISTENING 4 TCP [::]:8888 [::]:0 LISTENING 4 PS C:\Users\Administrator>
Install Web Access
PS C:\Users\Administrator> Install-WindowsFeature –Name WindowsPowerShellWebAccess -ComputerName MSERVER1 -IncludeManage mentTools -Restart Success Restart Needed Exit Code Feature Result ------- -------------- --------- -------------- True No Success {ASP.NET 4.5, Application Development, ASP... WARNING: For more information about how to finish installing and configuring Windows PowerShell Web Access, see http://go.microsoft.com/fwlink/?LinkID=221050. PS C:\Users\Administrator>
Config gateway
PS C:\Users\Administrator> Install-PswaWebApplication -UseTestCertificate WARNING: Using a test certificate in a production environment is not recommended for security reasons. This certificate should be used only for internal testing of Windows PowerShell Web Access. The test certificate expires in 90 days. Creating application pool pswa_pool... Name State Applications ---- ----- ------------ pswa_pool Started Creating web application pswa... Path : /pswa ApplicationPool : pswa_pool EnabledProtocols : http PhysicalPath : C:\Windows\Web\PowerShellWebAccess\wwwroot Creating self-signed certificate... Creating HTTPS binding... PS C:\Users\Administrator>
Add authorization rule
PS C:\Users\Administrator> hostname mserver1 PS C:\Users\Administrator> Add-PswaAuthorizationRule –UserName MSERVER1\Administrator -ComputerName MSERVER1 -Configurat ionName microsoft.powershell Id RuleName User Destination ConfigurationName -- -------- ---- ----------- ----------------- 0 Rule 0 MSERVER1\Administrator MSERVER1 microsoft.powershell PS C:\Users\Administrator> Add-PswaAuthorizationRule –UserName MSERVER1\CloudAdmin -ComputerName MSERVER1 -Configuration Name microsoft.powershell Id RuleName User Destination ConfigurationName -- -------- ---- ----------- ----------------- 1 Rule 1 MSERVER1\CloudAdmin MSERVER1 microsoft.powershell PS C:\Users\Administrator> Get-PswaAuthorizationRule Id RuleName User Destination ConfigurationName -- -------- ---- ----------- ----------------- 0 Rule 0 mserver1\administrator MSERVER1 microsoft.powershell 1 Rule 1 mserver1\cloudadmin MSERVER1 microsoft.powershell PS C:\Users\Administrator>
OR allow all user and configuration with
PS C:\Users\Administrator> Add-PswaAuthorizationRule –UserName * -ComputerName * -ConfigurationName * -- OR -- PS C:\Users\Administrator> Add-PswaAuthorizationRule * * *
Bingo !!!.
Open url => https://xx.xx.xx.xx/pswa
Welcome to PS session like below
Additional step for remote to machine in difference domain or non-domain manner.
Add trusted host
PS C:\Users\Administrator> Get-Item WSMan:\localhost\Client\TrustedHosts WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client Type Name SourceOfValue Value ---- ---- ------------- ----- System.String TrustedHosts STATBSPDB,172.19.3.13 PS C:\Users\Administrator> Set-Item WSMan:\localhost\Client\TrustedHosts -Value STATBBI -Force -Concatenate PS C:\Users\Administrator> Set-Item WSMan:\localhost\Client\TrustedHosts -Value 172.19.2.31 -Force -Concatenate PS C:\Users\Administrator> Get-Item WSMan:\localhost\Client\TrustedHosts WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client Type Name SourceOfValue Value ---- ---- ------------- ----- System.String TrustedHosts STATBSPDB,172.19.3.13,STATBBI,172.19.2.31
Test from local machine (gateway) when run Get-Credential, a windows is pop-up then enter a credential like "STATBBI\Administrator" and password of remote server.
PS C:\Users\Administrator> $C = Get-Credential cmdlet Get-Credential at command pipeline position 1 Supply values for the following parameters: Credential PS C:\Users\Administrator> Enter-PSSession -ComputerName STATBBI -Credential $C [STATBBI]: PS C:\Users\Administrator\Documents> [STATBBI]: PS C:\Users\Administrator\Documents> [STATBBI]: PS C:\Users\Administrator\Documents> hostname STATBBI
After that try to access from web by enter an info like below.
Sunday, January 15, 2017
Windows PowerShell Cmdlets
Get-Service -RequiredServices xxx Get-Service | Where-Object {$_.displayname -like "*Web*"} Get-Service | Sort-Object status,displayname Get-Service | Where-Object {$_.status -eq "running"} Get-Service | Where-Object {$_.status -eq "running" -and $_.displayname -like "*Application*"}Get-WmiObject win32_service | Where-Object {$_.displayname -like "SQL Server (MSSQLSERVER)"} | format-list *Get-Process Stop-Process -processname notepad Start-Service btwdins Stop-Service btwdins Restart-Service btwdins,alerter Stop-Computer -Force Stop-Computer -ComputerName "Server01", "Server02", "localhost" Stop-Computer –computer DC1 –Credential nwtraders\administrator Restart-Computer -WhatIf Restart-Computer "server01","server02","server03" Get-EventLog system -newest 5 | Sort-Object eventid -descendingGet-EventLog -LogName system -entrytype Error, Warning -After (Get-Date).AddDays(-3)PS C:\temp\PowerShell2> echo > test.ps1 cmdlet Write-Output at command pipeline position 1 Supply values for the following parameters: InputObject[0]: Get-EventLog system -newest 5 | Sort-Object eventid -descending InputObject[1]: Get-Process InputObject[2]: Get-Service InputObject[3]:Get-EventLog -LogName system -entrytype warning -After (Get-Date).AddDays(-3) | Where-Object {$_.Source -ne "disk"}Get-EventLog -Index 18308517 -Logname System | Format-List
PS C:\temp\PowerShell2> type test.ps1
Get-EventLog system -newest 5 | Sort-Object eventid -descending Get-Process Get-Service PS C:\temp\PowerShell2> powershell -ExecutionPolicy ByPass -File test.ps1
File Operation PowerShell
PS C:\SysInternals\sendmail> net use \\STATBSPDB\C$ password /USER:STATBSPDB\administrator The command completed successfully. PS C:\SysInternals\sendmail> Copy-Item \\STATBSPDB\c$\SysInternals\sendmail\EventDetail.txt . PS C:\SysInternals\sendmail> dir Directory: C:\SysInternals\sendmail Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 9/29/2009 11:02 AM 13589 CHANGELOG.txt -a--- 1/28/2017 11:00 PM 5 close-wait.txt -a--- 1/12/2017 1:16 PM 782 dbmem.sql -a--- 1/28/2017 11:01 PM 798 dbmem.txt -a--- 1/28/2017 11:20 PM 121856 EventDetail.txt -a--- 12/7/2016 3:15 PM 45501 eventlast3hrs.txt -a--- 1/28/2017 11:00 PM 255618 eventlast3hrs.xml -a--- 9/29/2009 11:03 AM 4701 README-BR.txt -a--- 9/29/2009 11:03 AM 8193 README.txt -a--- 9/29/2009 10:49 AM 1531966 sendEmail.exe -a--- 9/29/2009 1:46 PM 82448 sendEmail.pl -a--- 1/12/2017 3:02 PM 895 sendmail - Copy.cmd -a--- 1/20/2017 4:29 PM 907 sendmail.cmd -a--- 1/12/2017 2:57 PM 0 sqlcmd.log -a--- 9/29/2009 11:03 AM 1256 TODO.txt -a--- 5/13/2016 11:57 AM 66888 typeperf.htm -a--- 1/28/2017 11:00 PM 3594 typeperf.out -a--- 5/13/2016 12:22 PM 1170 typeperf.txt PS C:\SysInternals\sendmail> Copy-Item \\STATBSPDB\c$\SysInternals\sendmail\EventDetail_App.txt . PS C:\SysInternals\sendmail> dir PS C:\SysInternals\sendmail> Invoke-WebRequest -Uri "http://www.7-zip.org/a/7z1604-x64.msi" -OutFile "C:\SysInternals\7z1604-x64.msi" PS C:\SysInternals\sendmail> Start-Process "C:\SysInternals\7z1604-x64.msi" /qn -Wait PS C:\SysInternals\sendmail> cd 'C:\Program Files\7-Zip' PS C:\Program Files\7-Zip> .\7z.exe a C:\SysInternals\sendmail\EventDetail.zip C:\SysInternals\sendmail\EventDetail* 7-Zip [64] 16.04 : Copyright (c) 1999-2016 Igor Pavlov : 2016-10-04 Scanning the drive: 2 files, 29325476 bytes (28 MiB) Creating archive: C:\SysInternals\sendmail\EventDetail.zip Items to compress: 2 Files read from disk: 2 Archive size: 482093 bytes (471 KiB) Everything is Ok PS C:\Program Files\7-Zip> cd C:\SysInternals\sendmail\ PS C:\SysInternals\sendmail> dir Directory: C:\SysInternals\sendmail Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 9/29/2009 11:02 AM 13589 CHANGELOG.txt -a--- 1/29/2017 12:00 AM 5 close-wait.txt -a--- 1/12/2017 1:16 PM 782 dbmem.sql -a--- 1/29/2017 12:01 AM 798 dbmem.txt -a--- 1/28/2017 11:20 PM 121856 EventDetail.txt -a--- 1/29/2017 12:29 AM 482093 EventDetail.zip -a--- 1/28/2017 11:33 PM 29203620 EventDetail_App.txt -a--- 12/7/2016 3:15 PM 45501 eventlast3hrs.txt -a--- 1/29/2017 12:00 AM 260753 eventlast3hrs.xml -a--- 9/29/2009 11:03 AM 4701 README-BR.txt -a--- 9/29/2009 11:03 AM 8193 README.txt -a--- 9/29/2009 10:49 AM 1531966 sendEmail.exe -a--- 9/29/2009 1:46 PM 82448 sendEmail.pl -a--- 1/12/2017 3:02 PM 895 sendmail - Copy.cmd -a--- 1/20/2017 4:29 PM 907 sendmail.cmd -a--- 1/12/2017 2:57 PM 0 sqlcmd.log -a--- 9/29/2009 11:03 AM 1256 TODO.txt -a--- 5/13/2016 11:57 AM 66888 typeperf.htm -a--- 1/29/2017 12:00 AM 3595 typeperf.out -a--- 5/13/2016 12:22 PM 1170 typeperf.txt PS C:\SysInternals\sendmail> .\sendEmail -o -a EventDetail.zip -f spadmin@nso.go.th -t nutthaphon@gmail.com -s mailgw.nso.go.th:25 -u "Event Viewer" -m "FYI" Jan 29 00:30:30 statbspapp sendEmail.exe[213848]: Email was sent successfully! PS C:\SysInternals\sendmail>
SQL Server PowerShell
First of all, enable scripting restriction then load sqlps module.
PS C:\Users\Administrator\Documents> Set-ExecutionPolicy RemoteSigned PS C:\Users\Administrator\Documents> Import-Module sqlps -DisableNameChecking PS SQLSERVER:\>
Try it
PS SQLSERVER:\> Invoke-Sqlcmd -Query "SELECT GETDATE() AS TimeOfQuery;" -ServerInstance "STATBSPDB" –Username "sa" –Password "p@ssw0rd "-- or --
PS SQLSERVER:\> Invoke-Sqlcmd -ServerInstance . -Database master -Query "select GetDate()" Column1 ------- 19-Jan-17 2:54:07 PM
cd SQLSERVER:\SQL\STATBSPDB\DEFAULT\Databases PS SQLSERVER:\SQL\STATBSPDB\DEFAULT\Databases> cd WSS_Content PS SQLSERVER:\SQL\STATBSPDB\DEFAULT\Databases\WSS_Content> Invoke-Sqlcmd -Query "SELECT DB_NAME() as [Database]" WARNING: Using provider context. Server = STATBSPDB, Database = WSS_Content. Database -------- WSS_Content PS SQLSERVER:\SQL\STATBSPDB\DEFAULT\Databases\WSS_Content>
PS SQLSERVER:\SQL\STATBSPDB\DEFAULT\Databases\WSS_Content> ls tables -Force Schema Name Created ------ ---- ------- dbo AllDocs 02-May-14 3:46 PM dbo AllDocVersions 02-May-14 3:46 PM dbo AllFileFragments 02-May-14 3:46 PM dbo AllLinks 02-May-14 3:46 PM dbo AllLists 02-May-14 3:46 PM dbo AllListsAux 02-May-14 3:46 PM dbo AllListsItemCount 02-May-14 3:46 PM dbo AllListsPlus 02-May-14 3:46 PM dbo AllListUniqueFields 02-May-14 3:46 PM dbo AllLookupRelationships 02-May-14 3:46 PM dbo AllSites 02-May-14 3:46 PM dbo AllUserData 02-May-14 3:46 PM dbo AllUserDataJunctions 02-May-14 3:46 PM dbo AllWebParts 02-May-14 3:46 PM dbo AllWebs 02-May-14 3:46 PM
PS SQLSERVER:\SQL\STATBSPDB\DEFAULT\Databases\master> Set-Location SQLSERVER:\SQL\STATBSPDB\DEFAULT\Databases\master PS SQLSERVER:\SQL\STATBSPDB\DEFAULT\Databases\master> sql "select * from spt_monitor" WARNING: Using provider context. Server = STATBSPDB, Database = master. lastrun : 10-Feb-12 9:02:09 PM cpu_busy : 20 io_busy : 9 idle : 2707 pack_received : 39 pack_sent : 39 connections : 19 pack_errors : 0 total_read : 0 total_write : 0 total_errors : 0 PS SQLSERVER:\SQL\STATBSPDB\DEFAULT\Databases\master>
SharePoint PowerShell
Manual registration on Windows PowerShell
PS C:\Users\Administrator> C:\"Program Files"\"Common Files"\"microsoft shared"\"Web Server Extensions"\15\CONFIG\POWERSHELL\Registration\SharePoint.ps1
-- OR --
Add-PSSnapin Microsoft.SharePoint.Powershell
Networking PowerShell
Get-DnsClientCache Clear-DnsClientCache Get-DnsClientGlobalSetting Get-DnsClientServerAddress Resolve-DnsName xx.yy.zz -server 172.18.1.11 Get-NetIPAddress Get-NetIPInterface Get-NetRoute Get-NetTCPConnection Get-NetTCPSetting Get-NetAdapter Get-NetAdapterAdvancedProperty Get-NetAdapterStatistics
Subscribe to:
Posts (Atom)


