Categories
Sponsors
Archive
Blogroll
Badges
Community
|
Posted in Windows Powershell | No Comment | 2,839 views | 18/06/2013 09:35
This is an example to show you how to fetch data from Sharepoint.
You need to send your domain credetials to log in Sharepoint server.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| $Webclient = New-Object System.Net.WebClient
$Webclient.Credentials = Get-Credential
$Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
$AsyPortalURL = "http://sharepoint/_layouts/srchrss.aspx?k=" + $Server
$Webpage = $Webclient.DownloadString($AsyPortalURL)
$URL = "$WebPage" -match '<link>(.*?)</link>'
$ServerID = $Matches[1]
$ServerID = $ServerID.Split("ID=")[-1]
$ServerListesi = "http://sharepoint/Lists/Server Listesi/DispForm.aspx?ID=" + $ServerID
$Webpage = $Webclient.DownloadString($ServerListesi)
$Webpage = $Webpage | ? {$_.trim() -ne ""}
$Webpage = $Webpage -replace '\s+', ' '
$IPField = "$Webpage" -match '<!-- FieldName="IP" FieldInternalName="IP" FieldType="SPFieldText" --> (.*?) </td>'
$IPAddress = $Matches[1]
$TestCon = Test-Connection $IPAddress
if (!$TestCon)
{
$Value = $Server + "," + $IPAddress
Add-Content -Value $Value -Path C:\pingtest.txt
}
} |
$Webclient = New-Object System.Net.WebClient
$Webclient.Credentials = Get-Credential
$Servers = Get-Content C:\Servers.txt
foreach ($Server in $Servers)
{
$AsyPortalURL = "http://sharepoint/_layouts/srchrss.aspx?k=" + $Server
$Webpage = $Webclient.DownloadString($AsyPortalURL)
$URL = "$WebPage" -match '<link>(.*?)</link>'
$ServerID = $Matches[1]
$ServerID = $ServerID.Split("ID=")[-1]
$ServerListesi = "http://sharepoint/Lists/Server Listesi/DispForm.aspx?ID=" + $ServerID
$Webpage = $Webclient.DownloadString($ServerListesi)
$Webpage = $Webpage | ? {$_.trim() -ne ""}
$Webpage = $Webpage -replace '\s+', ' '
$IPField = "$Webpage" -match '<!-- FieldName="IP" FieldInternalName="IP" FieldType="SPFieldText" --> (.*?) </td>'
$IPAddress = $Matches[1]
$TestCon = Test-Connection $IPAddress
if (!$TestCon)
{
$Value = $Server + "," + $IPAddress
Add-Content -Value $Value -Path C:\pingtest.txt
}
}
As you see that, I’m getting ip addresses of servers. After that doing test-network check for that servers.
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 1,964 views | 17/06/2013 14:57
You can get your prod VMs list, Hyper-V hostname, clustername and csv paths like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
| $VMs = Get-SCVirtualMachine | where {$_.HostGroupPath -like "All Hosts\*Prod*"}
foreach ($VM in $VMs)
{
$VMName = $VM.Name
$VMHostname = $VM.Hostname
$VMHasPassthroughDisk = $VM.HasPassthroughDisk
$VMHost = Get-SCVMHost $VMHostname
$VMHostCluster = $VMHost.HostCluster
$VMHardDisk = @($VM | Get-VirtualHardDisk)[0].Location
$VMHardDiskLocation = $VMHardDisk.Split("\")[2]
$Value = $VMName + "," + $VMHostname + "," + $VMHostCluster + "," + $VMHardDiskLocation
Add-Content -Value $Value -Path "VMList.txt"
} |
$VMs = Get-SCVirtualMachine | where {$_.HostGroupPath -like "All Hosts\*Prod*"}
foreach ($VM in $VMs)
{
$VMName = $VM.Name
$VMHostname = $VM.Hostname
$VMHasPassthroughDisk = $VM.HasPassthroughDisk
$VMHost = Get-SCVMHost $VMHostname
$VMHostCluster = $VMHost.HostCluster
$VMHardDisk = @($VM | Get-VirtualHardDisk)[0].Location
$VMHardDiskLocation = $VMHardDisk.Split("\")[2]
$Value = $VMName + "," + $VMHostname + "," + $VMHostCluster + "," + $VMHardDiskLocation
Add-Content -Value $Value -Path "VMList.txt"
}
If you need to get all vms, just try “All Hosts\*” for all host groups.
Posted in Windows Powershell, Windows Server | No Comment | 5,362 views | 05/06/2013 11:20
You can export HP’s IML logs via PowerShell. Execute PowerShell console with Administrative privileges and run:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| $Hostname = $env:COMPUTERNAME
&"C:\Program Files\Compaq\Cpqimlv\cpqimlv.exe" /export:C:\test.iml /m:$Hostname
$Logs = Get-Content "C:\test.iml" | Select-String Critical,Caution
Foreach ($Log in $Logs)
{
$ErrorDesc = $Log.Line.Split('"')[1]
if ($ErrorDesc)
{
$ErrorType = $Log.Line.Split(",")[-4]
$ErrorDate = $Log.Line.Split(",")[-1]
}
if ($ErrorType -eq "Critical")
{
Write-Host "Description: $ErrorDesc" -ForegroundColor Red
Write-Host "Type: $ErrorType" -ForegroundColor Red
Write-Host "Date: $ErrorDate" -ForegroundColor Red
Write-Host " "
}
else
{
Write-Host "Description: $ErrorDesc" -ForegroundColor Yellow
Write-Host "Type: $ErrorType" -ForegroundColor Yellow
Write-Host "Date: $ErrorDate" -ForegroundColor Yellow
Write-Host " "
}
} |
$Hostname = $env:COMPUTERNAME
&"C:\Program Files\Compaq\Cpqimlv\cpqimlv.exe" /export:C:\test.iml /m:$Hostname
$Logs = Get-Content "C:\test.iml" | Select-String Critical,Caution
Foreach ($Log in $Logs)
{
$ErrorDesc = $Log.Line.Split('"')[1]
if ($ErrorDesc)
{
$ErrorType = $Log.Line.Split(",")[-4]
$ErrorDate = $Log.Line.Split(",")[-1]
}
if ($ErrorType -eq "Critical")
{
Write-Host "Description: $ErrorDesc" -ForegroundColor Red
Write-Host "Type: $ErrorType" -ForegroundColor Red
Write-Host "Date: $ErrorDate" -ForegroundColor Red
Write-Host " "
}
else
{
Write-Host "Description: $ErrorDesc" -ForegroundColor Yellow
Write-Host "Type: $ErrorType" -ForegroundColor Yellow
Write-Host "Date: $ErrorDate" -ForegroundColor Yellow
Write-Host " "
}
}
You can also write them into event viewer after parsing. If you need to get specific date, change script like this:
1
2
3
4
5
6
7
| $Date = Get-Date
$Day = $Date.Day
$Month = $Date.Month
$Year = $Date.Year
$Hostname = $env:COMPUTERNAME
&"C:\Program Files\Compaq\Cpqimlv\cpqimlv.exe" /export:C:\test.iml /m:$Hostname
$Logs = Get-Content "C:\test.iml" | Select-String Critical,Caution | Select-String $Month/$Day/$Year |
$Date = Get-Date
$Day = $Date.Day
$Month = $Date.Month
$Year = $Date.Year
$Hostname = $env:COMPUTERNAME
&"C:\Program Files\Compaq\Cpqimlv\cpqimlv.exe" /export:C:\test.iml /m:$Hostname
$Logs = Get-Content "C:\test.iml" | Select-String Critical,Caution | Select-String $Month/$Day/$Year
So you can always get new Critical and Caution logs.
Posted in Windows Powershell | No Comment | 20,719 views | 27/05/2013 10:35
I’ll show you how to get total elapsed time for processes in same output with using Label and Expression function of PowerShell. You can see the command below:
(Get-Process | ft Name,@{label="Elapsed Time";expression={[System.Math]::Round(((Get-Date)-$_.StartTime).TotalSeconds)}}) |
(Get-Process | ft Name,@{label="Elapsed Time";expression={[System.Math]::Round(((Get-Date)-$_.StartTime).TotalSeconds)}})
You can execute additional commands with Expression function in same PowerShell command.
Posted in Exchange Server, Windows Powershell | No Comment | 2,933 views | 24/05/2013 14:02
You can get your db statistics like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| # PoSHStats Pipeline
function PoSHStats-Pipeline
{
# Create Timer
Begin { $i=0; $Timer = [Diagnostics.StopWatch]::StartNew() }
# Process Items
Process { $i++; $_ }
# Informational Output
End { Write-Debug "Processed $i items in $($Timer.Elapsed.TotalSeconds) seconds." }
}
$ExchangeServer = "EXMB04"
# Get MailboxDatabase
if ($ExchangeServer.IsMailboxServer -eq $True)
{
Get-MailboxDatabase -Status | where {$_.Server -eq "$ExchangeHost"} | PoSHStats-Pipeline | %{
# Database Statistics
$DBName = $_.Name
$DBSize = $_.DatabaseSize.ToGB()
$DBLastFullBackup = $_.LastFullBackup.ToString()
$DBWhiteSpace = $_.AvailableNewMailboxSpace.ToMB()
# Mailbox Statistics
$MBStats = Get-MailboxStatistics -Database $DBName | %{$_.TotalItemSize.value.ToMb()} | Measure-Object -Average
$MBCount = $MBStats.Count
$MBAvg = ([math]::round(($MBStats.Average), 0))
# Mailbox Database Disk Usage
$DBDrive = $_.EdbFilePath.DriveName
$DBDiskInfo = Get-WmiObject -ComputerName $ExchangeHost -Class Win32_LogicalDisk | Where {$_.DeviceID -eq $DBDrive}
$DBDiskSize = ([math]::round(($DBDiskInfo.Size / 1GB), 0))
$DBDiskFreeSpace = ([math]::round(($DBDiskInfo.FreeSpace / 1GB), 0))
# Exchange Database Disk Usage
[int]$ExchangeDatabaseSize = [int]$ExchangeDatabaseSize + [int]$DBSize
[int]$ExchangeDatabaseWhiteSpace = [int]$ExchangeDatabaseWhiteSpace + [int]$DBWhiteSpace
# Database White Space
[int]$DatabaseWhiteSpace = ([math]::round(($DBWhiteSpace / 1KB), 0))
# Exchange Server Disk Usage
$TestDrive = $DiskDrives.Contains("$DBDrive")
if ($TestDrive -ne $True)
{
$DiskDrives.Add("$DBDrive") | Out-Null
[int]$ExchangeDatabaseDiskSize = [int]$ExchangeDatabaseDiskSize + [int]$DBDiskSize
[int]$ExchangeDatabaseDiskFreeSpace = [int]$ExchangeDatabaseDiskFreeSpace + [int]$DBDiskFreeSpace
}
# Host Total Database Size
[int]$HostTotalDatabaseSize = [int]$HostTotalDatabaseSize + [int]$DBSize
# Host Total Database Count
[int]$HostTotalDatabaseCount++ | Out-Null
# Host Total Mailbox Count
[int]$HostTotalMailboxCount = [int]$HostTotalMailboxCount + [int]$MBCount
# Host Total Mailbox Average
[int]$HostTotalMailboxAverage = [int]$HostTotalMailboxAverage + [int]$MBAvg
}
} |
# PoSHStats Pipeline
function PoSHStats-Pipeline
{
# Create Timer
Begin { $i=0; $Timer = [Diagnostics.StopWatch]::StartNew() }
# Process Items
Process { $i++; $_ }
# Informational Output
End { Write-Debug "Processed $i items in $($Timer.Elapsed.TotalSeconds) seconds." }
}
$ExchangeServer = "EXMB04"
# Get MailboxDatabase
if ($ExchangeServer.IsMailboxServer -eq $True)
{
Get-MailboxDatabase -Status | where {$_.Server -eq "$ExchangeHost"} | PoSHStats-Pipeline | %{
# Database Statistics
$DBName = $_.Name
$DBSize = $_.DatabaseSize.ToGB()
$DBLastFullBackup = $_.LastFullBackup.ToString()
$DBWhiteSpace = $_.AvailableNewMailboxSpace.ToMB()
# Mailbox Statistics
$MBStats = Get-MailboxStatistics -Database $DBName | %{$_.TotalItemSize.value.ToMb()} | Measure-Object -Average
$MBCount = $MBStats.Count
$MBAvg = ([math]::round(($MBStats.Average), 0))
# Mailbox Database Disk Usage
$DBDrive = $_.EdbFilePath.DriveName
$DBDiskInfo = Get-WmiObject -ComputerName $ExchangeHost -Class Win32_LogicalDisk | Where {$_.DeviceID -eq $DBDrive}
$DBDiskSize = ([math]::round(($DBDiskInfo.Size / 1GB), 0))
$DBDiskFreeSpace = ([math]::round(($DBDiskInfo.FreeSpace / 1GB), 0))
# Exchange Database Disk Usage
[int]$ExchangeDatabaseSize = [int]$ExchangeDatabaseSize + [int]$DBSize
[int]$ExchangeDatabaseWhiteSpace = [int]$ExchangeDatabaseWhiteSpace + [int]$DBWhiteSpace
# Database White Space
[int]$DatabaseWhiteSpace = ([math]::round(($DBWhiteSpace / 1KB), 0))
# Exchange Server Disk Usage
$TestDrive = $DiskDrives.Contains("$DBDrive")
if ($TestDrive -ne $True)
{
$DiskDrives.Add("$DBDrive") | Out-Null
[int]$ExchangeDatabaseDiskSize = [int]$ExchangeDatabaseDiskSize + [int]$DBDiskSize
[int]$ExchangeDatabaseDiskFreeSpace = [int]$ExchangeDatabaseDiskFreeSpace + [int]$DBDiskFreeSpace
}
# Host Total Database Size
[int]$HostTotalDatabaseSize = [int]$HostTotalDatabaseSize + [int]$DBSize
# Host Total Database Count
[int]$HostTotalDatabaseCount++ | Out-Null
# Host Total Mailbox Count
[int]$HostTotalMailboxCount = [int]$HostTotalMailboxCount + [int]$MBCount
# Host Total Mailbox Average
[int]$HostTotalMailboxAverage = [int]$HostTotalMailboxAverage + [int]$MBAvg
}
}
This is the preview code of PoSHStats Exchange Server module.
Posted in Windows Powershell | No Comment | 2,010 views | 24/05/2013 10:15
In this example, you can’t open notepad.exe more than once. New notepad.exe processes will be killed.
1
2
3
4
5
6
7
8
9
| $ShouldProcess = $true
While ($ShouldProcess)
{
$Processes = Get-Process -Name Notepad -EA SilentlyContinue
if ($Processes.Count -gt "1")
{
Get-Process -Name Notepad | Sort-Object StartTime | Select-Object -Skip 1 | Stop-Process
}
} |
$ShouldProcess = $true
While ($ShouldProcess)
{
$Processes = Get-Process -Name Notepad -EA SilentlyContinue
if ($Processes.Count -gt "1")
{
Get-Process -Name Notepad | Sort-Object StartTime | Select-Object -Skip 1 | Stop-Process
}
}
Also it’s possible to add delay between process kills.
Posted in Windows Powershell, Windows Server | No Comment | 2,693 views | 24/05/2013 09:33
First, you need to install PSTools on your server. Copy psexec.exe and pskill.exe into C:\ directory.
After that you can patch servers like this:
1
2
3
4
5
6
7
8
9
10
11
| $Servers = Get-Content "C:\Servers.txt"
foreach ($Server in $Servers)
{
$PSExecPath = "C:\psexec.exe"
$PSKillPath = "C:\pskill.exe"
$Command1 = "C:\cp017534.exe /s"
$Shell1 = $PSExecPath + " \\" + $Server + " " + $Command1
cmd.exe /c $Shell1
$Shell2 = $PSKillPath + " \\" + $Server + " PSEXESVC"
cmd.exe /c $Shell2
} |
$Servers = Get-Content "C:\Servers.txt"
foreach ($Server in $Servers)
{
$PSExecPath = "C:\psexec.exe"
$PSKillPath = "C:\pskill.exe"
$Command1 = "C:\cp017534.exe /s"
$Shell1 = $PSExecPath + " \\" + $Server + " " + $Command1
cmd.exe /c $Shell1
$Shell2 = $PSKillPath + " \\" + $Server + " PSEXESVC"
cmd.exe /c $Shell2
}
cp017534.exe is the hp’s firmware update. It’s located on my servers C:\ directory also.
|