16 Eylül 2020 Çarşamba

Powershell New-PSSession

Giriş
Invoke-Command -Session $remoteSession -ArgumentList(...) -ScriptBlock {...} şeklinde uzak makinede komut çalıştırmak içindir.

Session ile işimiz bitince Remove PSSession ile kapatılır

Örnek
Şöyle yaparızz
$username = "myuser"
$password = "mypassword"
$secureString = ConvertTo-SecureString $password -ASPlainText -Force


$credentials = New-Object System.Management.Automation.PSCredentials($username,
  $secureString)
$computer = "..."
$remoteSession = New-PSSession $computer -Credential $credentials
...
Remove PSSession -Session $remoteSession

10 Eylül 2020 Perşembe

Powershell Start-Job - Arka Planda İş Başlatır

Giriş
Bu komut arka planda iş başlatırken Invoke-Command ön planda iş başlatır.

Backtick
Backtick ile satırları bölmek iyi değil. Onun yerine zaten bölünebilecek yerlde bölmek lazım. Açıklaması şöyle.
The best thing is to leave out the line continuation character and simply break at the pipe, the comma, the brace, or the parenthesis because Windows PowerShell is expecting it anyway, and you avoid a potential source of failure.
ScriptBlock seçeneği
Örnek
Şöyle yaparız. scriptBlock arka planda çalıştırılacak metodu temsil eder. Bu metod bir parametre ile çağrılır
$Param1 = "..."
$Computers =(Get-Content "computers.txt")

foreach ($computer in $Computers) {

  $scriptBlock = {

    param($ScriptParam1)

    ...

  }

  Start-Job -Name "Job-$computer" -ScriptBlock $scriptBlock -ArgumentList ($Param1)

}


#wait for jobs to finish
Get-Job | Wait-Job


#Show output of jobs
Get-Job | Receive-Job


9 Eylül 2020 Çarşamba

Windows netsh komutu

interface ipv4 show address
Ağ arayüzlerimizin IPV4 adreslerini görmek için şöyle yaparız
netsh interface ipv4 show address
interface ipv4 show subinterfaces
Ağ arayüzlerimizin MTU değerlerini görmek için şöyle yaparız
>netsh interface ipv4 show subinterfaces

   MTU  MediaSenseState   Bytes In  Bytes Out  Interface
------  ---------------  ---------  ---------  -------------
4294967295                1          0          0  Loopback Pseudo-Interface 1
  1500                1  2180082896  106590535  Ethernet
Değiştirmek için şöyle yaparız
>netsh interface ipv4 set subinterface "Ethernet" mtu=1492 store=persistent

interface portproxy
port forwarding içindir. Açıklaması şöyle
It should not persist a reboot unless you specify store=persistent. You can delete the rule yourself by running similar to netsh interface portproxy delete v4tov4 listenaddress=192.168.x.y listenport=3306
Örnek
Açıklaması şöyle
Alternatively you could configure port forwarding to forward traffic destined for 192.168.x.y on port 3306 to 127.0.0.1 on port 3306 using syntax similar to the below command.
Şöyle yaparız
netsh interface portproxy add v4tov4 listenaddress=192.168.x.y listenport=3306
connectaddress=127.0.0.1 connectport=3306
Örnek
Şöyle yaparız. Burada kendi Window bilgisayarımda port forwarding yaparak, ssh isteklerini WSL 2  üzerinde çalışan SSH sunucusuna yönlendiriyorum.
netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=172.26.66.223
trace start çeneği
TCP paketlerini yakalamak içindir.
Örnek
Yakalamaya başlamak için şöyle yaparız
netsh trace start capture=yes tracefile=c:\temp\trace.etl
Durdurmak için şöyle yaparız
netsh trace stop

23 Ağustos 2020 Pazar

Windows find komutu

/I seçeneği
find /i ile "case sensitive" olmadığını belirtiriz. find /n ile satır numarasını alırız.

Örnek
Bir java uygulamasının ana sınıfını görmek için şöyle yaparız.
jcmd | find /I /N "sun.tools.jconsole.JConsole"


IF "%ERRORLEVEL%" GTR "0" (
    jconsole.exe
)

IF %ERRORLEVEL% EQU 0 (
    echo Programm is running 
) 

9 Ağustos 2020 Pazar

GetDriveType metodu

Giriş
Çağrı sonucu olarak şunu döner
DRIVE_UNKNOWN - 0 - The drive type cannot be determined.

DRIVE_NO_ROOT_DIR - 1 - The root path is invalid; for example, there is no volume mounted at the specified path.

DRIVE_REMOVABLE - 2 - The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader.

DRIVE_FIXED - 3 - The drive has fixed media; for example, a hard disk drive or flash drive.

DRIVE_REMOTE - 4 - The drive is a remote (network) drive.

DRIVE_CDROM - 5 - The drive is a CD-ROM drive.

DRIVE_RAMDISK - 6 - The drive is a RAM disk.
Örnek
Şöyle yaparız
#include <windows.h>
int main()
{
  wchar_t basePath[1024]{ L"" }, volName[1024]{ L"" };
  GetModuleFileName(NULL, basePath, 1024);
  GetVolumePathName(basePath, volName, 1024);

  UINT type = GetDriveType(volName);
  if (type == 2)
  {
    MessageBox(NULL, L"You are running from a flash drive (USB)",L"",MB_OK);
  }
  else
  {
    MessageBox(NULL, L"You are NOT running from a flash drive (USB)",L"", MB_OK);
  }
}

SetupDiGetClassDevs metodu

Giriş
  SetupDiGetClassDevs()
  SetupDiEnumDeviceInfo()

  SetupDiGetDeviceRegistryProperty()

çağrıları genellikle birlikte kullanılır

Örnek
Şöyle yaparız
HDEVINFO hdevinfo = SetupDiGetClassDevs(&GUID_DEVCLASS_DISKDRIVE,NULL, NULL,
  DIGCF_PRESENT);
if (hdevinfo == INVALID_HANDLE_VALUE) {
    WriteLog(L"hdevinfo is INVALID_HANDLE_VALUE");
    return USB_PROT_ERROR;
}
DWORD MemberIndex = 0;
SP_DEVINFO_DATA sp_devinfo_data;
ZeroMemory(&sp_devinfo_data, sizeof(sp_devinfo_data));
sp_devinfo_data.cbSize = sizeof(sp_devinfo_data);
while (SetupDiEnumDeviceInfo(hdevinfo, MemberIndex, &sp_devinfo_data)) {
  DWORD PropertyRegDataType;
  DWORD RequiredSize;
  TCHAR PropertyBuffer[500];
  //get the name of this device
  if (SetupDiGetDeviceRegistryProperty(hdevinfo, &sp_devinfo_data,
    SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, &PropertyRegDataType, (PBYTE)&PropertyBuffer,
     sizeof(PropertyBuffer), &RequiredSize)) {
        WriteLog(L"Device name: %s", PropertyBuffer);
    ...    
    }
}

22 Temmuz 2020 Çarşamba

Windows xcopy komutu

Giriş
- Bu komut yerine robocopy kullanılabilir.

- xcopy kopyalama yaparken bulduğu dosyanın relative path'ini de muhafaza eder. c:\d1\d2\f1.txt dosyasını c:\d1 altından başlayarak kopyalarsak c:\target\d2\f1.txt şeklide kopyalar. Yani d2\f1.txt yolunu muhafaza eder.

Wildcard Kullanımı
Örnek - Dosya İsmi İçeriyorsa (Contains)
Şöyle yaparız. messages.properties veya messages_tr.properties gibi dosyaları bulur. Alt dizinleri de dolaşır
xcopy *messages*.properties c:\target /SY
Örnek - Dosya İsmi Sonu (Suffix)
Şöyle yaparız. ".1.txt" gibi nokta + herhangi bir karakter + nokta + txt isimli dosyaları bulur
xcopy /Y "C:\*.?.txt" result.txt
/EXCLUDE seçeneği
Bu seçenek ile maaleself wildcard kullanılamıyor. Şu komut çalışmaz
xcopy /Y "C:\*.txt" /EXCLUDE:"*tmp*" result.txt
Eğer hariç bırakılacak şeyleri ayrı bir dosyaya satır satır yazarsak şöyle yaparız
xcopy /Y "C:\*.txt" /EXCLUDE:exclusions.txt result.txt
/I seçeneği
/I hedef  dizin yoksa yaratır

/S seçeneği
/S boş dizinler hariç tüm dizin ve altdizinleri kopyalar

/Y seçeneği
/Y hedef dosyalar varsa üzerine yazar