Categories
Sponsors
Archive
Blogroll
Badges
Community
|
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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,486 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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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,805 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 |
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 |
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 |
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,894 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()
} |
# 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,870 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() |
# 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 $_
} |
# 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.
Posted in Windows Powershell | No Comment | 1,676 views | 02/08/2012 07:10
Will Kruss reported an important bug of SetLinuxVM v4.4.
It’s not working on Windows Server 2008 and R2 due to PowerShell v2.
I’ve changed my development environment to Windows Server 2012 and Powershell v3. But I’ve fixed all compability issues.
Please download SetLinuxVM v4.5 for PowerShell v2 support.
You can also upgrade your Powershell version on Windows Server 2008 R2 to Powershell v3.
Posted in Windows Powershell | No Comment | 1,613 views | 01/08/2012 13:42
C# ile ilgili kendime bazı notlarım:
String Builder:
StringBuilder SB = new StringBuilder();
SB.AppendFormat("Get-VM -Id {0} | Set-VM ", Id); |
StringBuilder SB = new StringBuilder();
SB.AppendFormat("Get-VM -Id {0} | Set-VM ", Id);
If / Else yazma:
if (ProcessorCount > 0)
SB.AppendFormat("-ProcessorCount {0} ", ProcessorCount); |
if (ProcessorCount > 0)
SB.AppendFormat("-ProcessorCount {0} ", ProcessorCount);
Or kullanımı ve eğer null ise:
if (string.IsNullOrEmpty(MemoryStartupBytes) || string.IsNullOrEmpty(MemoryStartupBytes))
sadsadsadasdada
else if (bla bla)
sdsdsadsadsada
else |
if (string.IsNullOrEmpty(MemoryStartupBytes) || string.IsNullOrEmpty(MemoryStartupBytes))
sadsadsadasdada
else if (bla bla)
sdsdsadsadsada
else
Class yaratma:
class NewVMResult
{
public string VMId { get; set; }
public string VMName { get; set; }
} |
class NewVMResult
{
public string VMId { get; set; }
public string VMName { get; set; }
}
Powershell çalıştırma:
static NewVMResult NewVM(string Name, string Path)
{
IEnumerable<dynamic> PSResult = PSShell.AddCommand("New-VM")
.AddParameter("Name", Name)
.AddParameter("Path", Path)
.Invoke();
var PSObject = PSResult.FirstOrDefault();
return new NewVMResult() { VMId = PSObject.Id.ToString(), VMName = PSObject.VMName };
} |
static NewVMResult NewVM(string Name, string Path)
{
IEnumerable<dynamic> PSResult = PSShell.AddCommand("New-VM")
.AddParameter("Name", Name)
.AddParameter("Path", Path)
.Invoke();
var PSObject = PSResult.FirstOrDefault();
return new NewVMResult() { VMId = PSObject.Id.ToString(), VMName = PSObject.VMName };
}
C# yeni başladığım için kendime not düştüm :)
|