5 Ekim 2020 Pazartesi

Windows powershell komutu

Giriş
Powershell kodlamak için Notepad++ ile kod yazıp çalıştırmak yerine IDE'yi kullanmak daha kolay olabilir

1. IDE
Powershell simgesine sağ tıklayıp "Windows PowerShell ISE" seçilirse IDE açılır. 

2. powershell komutu
Windows + R tuşuna basıp cmd.exe yazarız. Açılan siyah pencereye powershell.exe yazarak powershell komutları işletilir.

Örnek
Şöyle yaparız.
powershell.exe "(get-physicaldisk).MediaType"
-ExecutionPolicy seçeneği
Bypass şeklinde kullanırsak powershell script'inin imzalı olması gerekmez. cmd.exe ile komut satırını açtıktan sonra şöyle yaparız.
powershell.exe -ExecutionPolicy Bypass -File myscript.ps

4 Ekim 2020 Pazar

Windows shutdown komutu

Giriş
GUI'den kapatılan windows 8 elektriği kesmiyor. Sadece hibernate ediyor. USB'lerde halen elektrik olabiliyor. Sistemi tamamen kapatmak ve elektriği kesmek için bu komut kullanılabilir.

/f seçeneği
Force anlamına gelir. Açıklaması şöyle
/f Force running applications to close without forewarning users.
/s seçeneği
Açıklaması şöyle.
Generally shutdown /s does not send any "close" messages. It call one of few Win32 API functions for shutting down Windows system.

These functions in turn notify aplications about shutdown/restart event via WM_QUERYENDSESSION and WM_ENDSESSION messages.

Programs can then agree or try to prevent pending shutdown by responsing properly to WM_QUERYENDSESSION, eg. for stopping cleanly, etc.

So, in theory after issue shutdown /s command, there is some "close" messages send to all programs and you don't need doing anything special for that.
Örnek
Şöyle yaparız.
shutdown /s /f /t 0
/t seçeneği
/t 0 "now" anlamına gelir. Açıklaması şöyle.
/t xxx Set the time-out period before shutdown to xxx seconds. The valid range is 0-315360000 (10 years), with a default of 30. If the timeout period is greater than 0, the /f parameter is implied.
Eğer t değeri 0 verilirse shutdown iptal edilebilir. Açıklaması şöyle.
If you want applications to be able to cancel the shutdown, you need to use shutdown /s /t 0 instead.
Bir başka açıklama şöyle.
BUT if you specify any timeout period greater than 0, or leave the timeout unspecified (as in shutdown /s on its own), Windows will actually kill your processes without offering you the chance to intervene. Incredible as it sounds, this is true, and is even by design, as shown by the usage output from shutdown /?.





29 Eylül 2020 Salı

Powershell Write-Host

Giriş
Açıklaması şöyle
In a nutshell, Write-Host writes to the console itself. Think of it as a MsgBox in VBScript. Write-Output, on the other hand, writes to the pipeline, so the next command can accept it as its input.

Powershell Read-Host

Giriş
Bu komutun tersi Write-Host. Read-Host konsoldan girdi okur

Örnek
Şöyle yaparız
$software = Read-Host "Software you want to remove"

23 Eylül 2020 Çarşamba

Powershell Function Tanımlama

Giriş
Powershell ile ScriptBlock tanımlanabiliyor. Ayrıca Function da tanımlanabiliyor.
ScriptBlock Invoke-Command -ScriptBlock $MyScriptBlock şeklinde kullanıbiliyor.

ScriptBlock Tanımlama
Örnek
Şöyle yaparız
$Credentials = ...
$Computeters = ...

foreach ($Computer in $Computers

  $MyScriptBlock = {
    param($Computer,$Credentials)
    ...
  }
  
  Invoke-Command -ScriptBlock $MyScriptBlock -ArgumentList ($Computer, $Credentials)
}
Örnek
Şöyle yaparız
function Check-Update {
  Param(
    [Parameter(Mandatory=$True, Valuefrompipeline=$True)]
[Validatenotnullorempty()]
[String]$Path ) $IsGood=$false if ($(Test-Path $Path) -and $Path.EndsWith(".msu")) { ... } else { Write-Error -Message "Update file: $path doesn't exist as a valid update file" } }
Örnek - Aynı script
Şöyle yaparız
Function New-Ping {
    
    Param(
        [Parameter(Mandatory)]
        [string]$ComputerName,
        [Parameter(Mandatory)]
        [int]$Intervall
    )

    while ($true) { 
        Test-Connection $ComputerName -Count 1
        Start-Sleep -MilliSeconds $Intervall 
    }
}
Çağırmak için şöyle yaparız
New-Ping ServerName 500

Windows ping komutu

Giriş 
Açıklaması şöyle
By default the time delay between two pings is equal to 1 second.

Powershell Test-Connection

Örnek 
Şöyle yaparız
while ($true) { Test-Connection ServerName -Count 1 ; Start-Sleep -MilliSeconds 500 }