search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Posted in Virtual Machine Manager, Windows Powershell, Windows Server | 1 Comment | 3,992 views | 25/08/2012 22:01

I’m re-coding PoSHStats for Cluster support. Currently it just supports standalone hosts, but i have to extend that support for cluster environments. So while i’m playing with Powershell, I want to add some of my codes.

Getting Hyper-V hostname:

$Hostname = gc env:computername

Getting Cluster name of your Hyper-V host:

$ClusterName = (Get-Cluster | Get-ClusterNode | where Name -eq $Hostname).Cluster.Name

Even better one for Hyper-V specifically:

$ClusterName = (@(Get-Cluster | Get-ClusterNode | where Name -eq $Hostname | Get-ClusterGroup | where GroupType -eq "VirtualMachine") | Select-Object -First 1).Cluster.Name

Also I see that this doesn’t work as expected on PowerShell v3, so don’t use something like this:

$ClusterName = @(Get-Cluster | Get-ClusterNode | where Name -eq $Hostname | Get-ClusterGroup | where GroupType -eq "VirtualMachine").Cluster.Name[0]

Getting all clustered virtual machines:

$VMs = Get-Cluster $ClusterName | Get-ClusterGroup | Where GroupType -eq "VirtualMachine" | Get-VM

Also you can extend your query for resource monitoring:

$VMs = Get-Cluster -Name $ClusterName | Get-ClusterGroup | Where GroupType -eq "VirtualMachine" | Get-VM | where ResourceMeteringEnabled -eq $True | Measure-VM

Getting free space of Cluster Shared Volume:

1
2
3
$CSVVolume = "Volume1"
$CSVDisk = Get-ClusterSharedVolume | Select -Expand SharedVolumeInfo | Where FriendlyVolumeName -match "$CSVVolume"					
[int]$HostDiskDriveFreeSpace = ([math]::round((($CSVDisk | Select -Expand Partition).FreeSpace / 1GB), 0))

Getting total disk space of Cluster Shared Volume:

1
2
3
$CSVVolume = "Volume1"
$CSVDisk = Get-ClusterSharedVolume | Select -Expand SharedVolumeInfo | Where FriendlyVolumeName -match "$CSVVolume"					
[int]$HostDiskSpace = ([math]::round((($CSVDisk | Select -Expand Partition).Size / 1GB), 0))

I’ll share more next days, keep watching..


Posted in Windows Powershell | 2 Comments | 11,683 views | 11/08/2012 17:01

I’ve used this script on my one of the projects to get last file of FTP directory.
So you always get latest log file in a directory with Powershell.

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
# FTP Config
$FTPHost = "10.10.10.20"
$Username = "ftpusr"
$Password = "12345678"
$FTPFolder = "%2f/my/log/path"
 
do
{
	# List Directory
	$FTPRequest = [System.Net.FtpWebRequest]::Create("$FTPFolderUrl") 
	$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username, $Password) 
	$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
 
	# Get Last Log File
	$FTPResponse = $FTPRequest.GetResponse()
	$ResponseStream = $FTPResponse.GetResponseStream()
	$FTPReader = New-Object System.IO.Streamreader -ArgumentList $ResponseStream
	do
	{
		try
		{
			$LogData = $FTPReader.ReadToEnd()
			Add-Content -Value $LogData -Path "$ScriptPath\FileList.txt"
			$LastLogFile = Get-Content -Path "$ScriptPath\FileList.txt" | ? {$_.trim() -ne ""} | Select -Last 1
			Clear-Content -Path "$ScriptPath\FileList.txt"
		}
		catch
		{
			Write-Debug $_
		}
	}
	while ($FTPReader.ReadLine() -ne $null)
	$FTPReader.Close()
 
	if ($LastLogFile -ne $null -and $LastLogFile -ne "" -and $LastLogFile -ne " ")
	{
		$ShouldProcess = "Continue"
	}
}
while ($ShouldProcess -ne "Continue")
 
Write-Host "Log File: $LastLogFile"

$LastLogFile is the name of the latest log file.


Posted in Windows Powershell | 2 Comments | 2,358 views | 04/08/2012 21:41

Bölüm 2 ile devam ediyorum çok kullanılan string işlemlerine:

String içerisinde geçen noktaları değiştirme:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.Replace(".","/")
yusufozturk/info

String içerisinde bulunan boşlukları yok etme:

1
2
3
PS C:\Users\Administrator> $string = "yusuf ozturk info"
PS C:\Users\Administrator> $string.Replace(" ","")
yusufozturkinfo

String başındaki tüm boşlukları yok edebilmek için bir diğer yol:

1
2
3
PS C:\Users\Administrator> $string = "           yusuf ozturk info"
PS C:\Users\Administrator> $string.TrimStart(" ")
yusuf ozturk info

İçeriği çekilen bir dosya içerisindeki boş satırları yok etme:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PS C:\Users\Administrator> get-content C:\test.txt
yusufozturk
 
info
 
test
 
string
 
PS C:\Users\Administrator> get-content C:\test.txt | ? {$_.trim() -ne ""}
yusufozturk
info
test
string

Büyük karakterleri küçültmek:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.ToLower();
yusufozturk.info

String başında geçen kelimeyi çıkartmak:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.trimstart("yusufozturk")
.info

String sonunda geçen kelimeyi çıkartmak:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.trimend(".info")
yusufozturk

Bölüm 2 için aklıma gelen örnekler de bu kadar.


Posted in Windows Powershell | No Comment | 2,489 views | 04/08/2012 21:25

Powershell ile en çok kullanılan trim, remove ve substring işlemlerini yazmak istedim.

Bir string’in ilk 4 harfini almak:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.Substring(0,4)
yusu

İlk 4 harfi alabilmek için bir diğer yol:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.Remove(4)
yusu

Bir string’in ilk 5 harfi dışında kalanları almak:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.Substring("0,5")
ozturk.info

İlk 5 harfi çıkartmak için bir diğer yol:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.Remove(0,5)
ozturk.info

2. ve 8. harfler arasını çıkartmak için:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.Remove(2,8)
yuk.info

Sadece 4. karakteri aradan çıkartmak için:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.Remove(2,8)
yuk.info

En baştan ve en sondan 4 karakteri çıkartmak:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.Substring(4,4)
fozt

En son karakteri çıkartmak:

1
2
3
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string -replace ".$"
yusufozturk.inf

Şuan için aklıma gelen örnekler bu şekilde. Eğer merak ettiğiniz başka bir işlem varsa sorabilirsiniz.


Posted in Windows Powershell | No Comment | 2,808 views | 04/08/2012 21:08

Powershell ile en çok kullanılan Split işlemlerini yazmak istedim.

Split örnekleri:

1
2
3
4
5
6
7
8
PS C:\Users\Administrator> $string = "yusufozturk.info"
PS C:\Users\Administrator> $string.split(".")
yusufozturk
info
PS C:\Users\Administrator> $string.split(".")[0]
yusufozturk
PS C:\Users\Administrator> $string.split(".")[1]
info

Ayırım noktasının çok olduğu durumlarda:

1
2
3
4
5
6
7
8
9
PS C:\Users\Administrator> $string = "yusuf.ozturk.info"
PS C:\Users\Administrator> $string.split(".")
yusuf
ozturk
info
PS C:\Users\Administrator> $string.split(".")[2]
info
PS C:\Users\Administrator> $string.split(".")[-1]
info

[-1] ile en sonda bulunan string parçasını görebiliyoruz.
En başta bulunan string parçacığı için ise [0] kullanmalısınız.

Eğer birden fazla noktadan ayırma yapmak istiyorsanız, bu şekilde kullanabilirsiniz.

1
2
3
4
5
6
7
8
9
10
PS C:\Users\Administrator> $string = "\\yusufozturk.info\inetpub\www"
PS C:\Users\Administrator> $string.Split(".\")
 
 
yusufozturk
info
inetpub
www
PS C:\Users\Administrator> $string.Split(".\")[-3]
info

Bu en çok kullandığım Split işlemleri bu şekilde.


Posted in Windows Powershell | No Comment | 5,897 views | 04/08/2012 20:52

You can download a file from FTP and you can use “foreach” to process every line of the file.

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
# FTP Config
$FTPHost = "10.10.10.5"
$Username = "admin"
$Password = "12345678"
$FTPFile = "log/test.log"
 
# FTP Log File Url
$FTPFileUrl = "ftp://" + $FTPHost + "/" + $FTPFile
 
# Create FTP Connection
$FTPRequest = [System.Net.FtpWebRequest]::Create("$FTPFileUrl") 
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username, $Password) 
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UsePassive = $false
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
 
# Get FTP File
$FTPResponse = $FTPRequest.GetResponse()
$ResponseStream = $FTPResponse.GetResponseStream()
$FTPReader = New-Object System.IO.Streamreader -ArgumentList $ResponseStream
do
{
	Write-Host $FTPReader.ReadLine()
}
while ($FTPReader.ReadLine() -ne $null)
$FTPReader.Close()
}

I used “do-while” to output each line of file.


Posted in Windows Powershell | 1 Comment | 14,873 views | 04/08/2012 20:46

Here is an example to open connection to Oracle with Powershell:

1
2
3
4
5
6
7
8
9
10
11
12
13
# Load Oracle Assembly
$LoadOracle = [Reflection.Assembly]::LoadFile("D:\oracle\product\10.2.0\db_1\ODP.NET\bin\2.x\Oracle.DataAccess.dll")
 
# Database Information
$DBName = "oracledb"
$DBUsername = "system"
$DBPassword = "password"
$DBTable = "mydatabase"
 
# Connect to Oracle
$SQLConnString = "User Id=$DBUsername;Password=$DBPassword;Data Source=$DBName"
$SQLConnection = New-Object Oracle.DataAccess.Client.OracleConnection($SQLConnString)
$SQLConnection.Open()

Now we can try a query example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# INSERTION_DATE,VPN_ID,CALLING_NUMBER,CALLED_NUMBER,SESSION_TIME,DISCONNECT_CAUSE,VPN_NAME,STARTTIME,STOPTIME
$SQLQuery = "insert into " + $DBTable + " (INSERTION_DATE,VPN_ID,CALLING_NUMBER,CALLED_NUMBER,SESSION_TIME,DISCONNECT_CAUSE,VPN_NAME,STARTTIME,STOPTIME) values "
$SQLQuery += "(to_date('" + $INSERTION_DATE + "','mm/dd/yyyy hh24:mi:ss')"
$SQLQuery += ",'" + $VPN_ID + "'"
$SQLQuery += ",'" + $CALLING_NUMBER + "'"
$SQLQuery += ",'" + $CALLED_NUMBER + "'"
$SQLQuery += ",'" + $SESSION_TIME + "'"
$SQLQuery += ",'" + $DISCONNECT_CAUSE + "'"
$SQLQuery += ",'" + $VPN_NAME + "'"
$SQLQuery += ",to_date('" + $STARTTIME + "','mm/dd/yyyy hh24:mi:ss')"
$SQLQuery += ",to_date('" + $STOPTIME + "','mm/dd/yyyy hh24:mi:ss'))"
 
# IMPORT TO DATABASE
try
{
$SQLCommand = New-Object Oracle.DataAccess.Client.OracleCommand($SQLQuery,$SQLConnection)
$SQLReader = $SQLCommand.ExecuteReader()
}
catch
{
Write-Host $_
}

It’s similar to Microsoft SQL process on Powershell.