គំនិត: សំណួរដែលសួរញឹកញាប់បំផុតអំពី PowerShell ។
អ្នកអាចប្រើបញ្ជីក្នុងវិធីផ្សេងគ្នានេះ:
- ចម្លង/បិទភ្ជាប់ពាក្យបញ្ជាមួយទៅក្នុងស្គ្រីប
- ដើម្បីមើលវាក្យសម្ព័ន្ធនៃពាក្យបញ្ជាជាក់លាក់មួយយ៉ាងលឿន
- ដើម្បីបង្កើននូវចំនេះដឹងបច្ចេកទេសរបស់អ្នក
- ដើម្បីរកឱ្យឃើញពាក្យបញ្ជាថ្មី
- ដើម្បីរៀបចំសម្ភាសន៍ការងារមួយ
ធ្វើឱ្យទាន់សម័យ |
ខែកក្កដា 02, 2015
|
អ្នកនិពន្ធ | powershell-guru.com |
ប្រភព | khmer.powershell-guru.com |
ប្រភេទ |
75
|
សំនួរនានា |
610
|
System
តើធ្វើដូចម្តេចដើម្បីកំណត់កំណែនៃ PowerShell របស់ខ្ញុំ?
1 2 3 4 5 6 7 8 9 |
# via Powershell $PSVersionTable.PSVersion.Major # via Registry (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine').PowerShellVersion # Versions 1 and 2 (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine').PowerShellVersion # Versions 3 and 4 # via Remote Invoke-Command -ComputerName $computer -ScriptBlock { $PSVersionTable.PSVersion.Major } |
តើធ្វើដូចម្តេចដើម្បីដំណើការ PowerShell នៅក្នុងកំណែផ្សេងមួយទៀតសម្រាប់ការត្រឡប់ទៅវិញនៃភាពឆបគ្នា?
powershell.exe -Version 2.0
តើធ្វើដូចម្តេចដើម្បីទាមទារឱ្យ PowerShell មានកំណែតិចតួចបំផុត (3.0 និងខ្ពស់ជាងនេះ) ក្នុងស្គ្រីបជាមួយ PowerShell?
#Requires -Version 3.0
តើធ្វើដូចម្តេចដើម្បីទាមទារឱ្យមានសិទ្ធិជាអ្នកគ្រប់គ្រងសម្រាប់ស្គ្រីបជាមួយ PowerShell?
1 2 3 4 5 |
# Solution 1 #Requires -RunAsAdministrator # Solution 2 [bool]((whoami.exe /all) -match 'S-1-16-12288') |
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលប៉ារ៉ាម៉ែត្រនៃស្គ្រីបជាមួយ PowerShell?
help -Name .\Get-ExchangeEnvironmentReport.ps1 -Full
តើធ្វើដូចម្តេចដើម្បីទទួលបានពត៌មានសម្រាប់អ្នកប្រើបច្ចុប្បន្នជាមួយ PowerShell?
[Security.Principal.WindowsIdentity]::GetCurrent()
តើធ្វើដូចម្តេចដើម្បីបង្កើត កែសម្រួល និងផ្ទុកទម្រង់ជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 |
# Create New-Item -Type file -Force $profile # Edit notepad.exe $profile # Reload (without restarting Powershell) & $profile .$profile |
តើធ្វើដូចម្តេចដើម្បីធ្វើការផ្អាក 5 វិនាទី/នាទីនៅក្នុងស្គ្រីបជាមួយ PowerShell?
Start-Sleep -Seconds 5
Start-Sleep -Seconds 300 # 5 minutes
តើធ្វើដូចម្តេចដើម្បីទទួលបានពេលវេលាចាប់ផ្ដើមមុនជាមួយ PowerShell?
(Get-CimInstance -ClassName win32_operatingsystem).LastBootUpTime
តើធ្វើដូចម្តេចដើម្បីទទួលបានប្រភេទឧបករណ៍បង្កើនល្បឿនជាមួយ PowerShell?
1 |
[PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get.GetEnumerator() | Select-Object -Property @{Name='Key'; Expression={$_.Key}},@{name='Value'; Expression={$_.Value}} | Sort-Object -Property Key | Format-Table -AutoSize |
តើធ្វើដូចម្តេចដើម្បីរាយកម្មវិធីចាប់ផ្ដើមជាមួយ PowerShell?
1 |
Get-WmiObject -Class Win32_StartupCommand | Sort-Object -Property Caption | Format-Table -Property Caption, Command, User -AutoSize |
តើធ្វើដូចម្តេចដើម្បីដំឡើងកម្មវិធីជាមួយ PowerShell?
1 2 |
$application = Get-WmiObject -Query "SELECT * FROM Win32_Product WHERE Name LIKE 'HP Recovery Manager'" $application.Uninstall() |
តើធ្វើដូចម្តេចដើម្បីយករូបថតអេក្រង់នៃផ្ទៃតុទាំងមូលឬមួយជាបង្អួចសកម្មជាមួយ PowerShell?
Take-ScreenShot -Screen -File 'C:\scripts\screenshot.png' -Imagetype JPEG
Repository : Take-ScreenShot
តើធ្វើដូចម្តេចដើម្បីទទួលបានការរាប់សារសម្រាប់ជួរ MSMQ ជាមួយ PowerShell?
1 |
Get-WmiObject -Class Win32_PerfRawData_MSMQ_MSMQQueue -ComputerName $computer | Format-Table -Property Name, MessagesInQueue -AutoSize |
តើធ្វើដូចម្តេចដើម្បីកំណត់គោលការណ៍ប្រតិបត្តិការជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 10 11 |
# Restricted - No scripts can be run. Windows PowerShell can be used only in interactive mode. Set-ExecutionPolicy -ExecutionPolicy Restricted # AllSigned - Only scripts signed by a trusted publisher can be run. Set-ExecutionPolicy -ExecutionPolicy AllSigned # RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they can be run. Set-ExecutionPolicy -ExecutionPolicy RemoteSigned # Unrestricted - No restrictions - All Windows PowerShell scripts can be run. Set-ExecutionPolicy -ExecutionPolicy Unrestricted |
តើធ្វើដូចម្តេចដើម្បីបង្កើតផ្លូវកាត់ជាមួយ PowerShell?
1 2 3 4 |
$shell = New-Object -ComObject WScript.Shell $shortcut = $shell.Createshortcut("$HOME\Desktop\Procexp.lnk") $shortcut.TargetPath = 'C:\SysinternalsSuite\procexp.exe' $shortcut.Save() |
របៀបបង្ខំឬ unpin កម្មវិធីដើម្បីរបារភារកិច្ចជាមួយ PowerShell?
1 2 3 4 |
$shell = New-Object -ComObject shell.application $program = $shell.Namespace($env:windir).Parsename('notepad.exe') $program.Invokeverb('TaskbarPin') $program.Invokeverb('TaskbarUnpin') |
តើធ្វើដូចម្តេចដើម្បីបើកកម្មវិធី Windows Explorer ជាមួយ PowerShell?
[Diagnostics.Process]::Start('explorer.exe')
Invoke-Item -Path C:\Windows\explorer.exe
តើធ្វើដូចម្តេចដើម្បីរាយកម្មវិធីបញ្ជាឧបករណ៍ការជាមួយ PowerShell?
Get-WmiObject -Class Win32_PnPSignedDriver
Get-WindowsDriver -Online -All
driverquery.exe
របៀបបង្កើតណែនាំជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 10 11 |
# Empty GUID $guid = [GUID]::Empty # New GUID (lower case by default) $guid = [GUID]::NewGuid() # New GUID (upper case) $guid = ([GUID]::NewGuid()).ToString().ToUpper() # New GUID with a specific value $guid = [GUID]('bc4ad3d3-d704-4bd0-843f-d607fbbc4cd7') |
តើធ្វើដូចម្តេចដើម្បីទទួលបានទីតាំងនៃថតបណ្តោះអាសន្នសម្រាប់អ្នកប្រើបច្ចុប្បន្នជាមួយ PowerShell?
[System.IO.Path]::GetTempPath()
តើធ្វើដូចម្តេចដើម្បីចូលរួមផ្លូវនិងផ្លូវក្មេងម្នាក់ចូលទៅក្នុងផ្លូវតែមួយជាមួយ PowerShell?
Join-Path -Path C:\ -ChildPath \windows
តើធ្វើដូចម្តេចដើម្បីរាយ cmdlets ទាំងអស់ “Get- *” ជាមួយ PowerShell?
Get-Command -Verb Get
តើធ្វើដូចម្តេចដើម្បីរាយថតដែលមានប្រព័ន្ធពិសេសជាមួយ PowerShell?
1 |
[System.Enum]::GetNames([System.Environment+SpecialFolder]) | ForEach-Object -Process { $_ + " [System.Environment]::GetFolderPath($_)" } |
តើធ្វើដូចម្តេចដើម្បីដាក់ឯកសារ ISO/VHD ជាមួយ PowerShell?
Mount-DiskImage 'D:\ISO\file.iso' # ISO
Mount-DiskImage 'D:\VHD\file.vhd' # VHD
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលកំណែក្របខណ្ឌដែលបានដំឡើងជាមួយ PowerShell?
1 |
Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name Version -EA 0 | Where-Object -FilterScript { $_.PSChildName -match '^(?!S)\p{L}' } | Select-Object -Property PSChildName, Version |
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលថាតើកំណែដែលក្របខណ្ឌ 4.5 ត្រូវបានដំឡើងជាមួយ PowerShell?
(Get-ItemProperty -Path 'HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full' -EA 0).Version -like '4.5*'
តើធ្វើដូចម្តេចដើម្បីចាប់ផ្តើមនិងបញ្ឈប់ប្រតិចារិកមួយ (ដើម្បីបង្កើតកំណត់ត្រានៃការ Windows PowerShell សម័យមួយ) ជាមួយ PowerShell?
Start-Transcript -Path 'C:\scripts\transcript.txt
Stop-Transcript
តើធ្វើដូចម្តេចដើម្បីផ្លាស់ប្តូរថតបច្ចុប្បន្នទៅទីតាំងជាក់លាក់មួយជាមួយ PowerShell?
Set-Location -Path 'C:\scripts'
តើធ្វើដូចម្តេចដើម្បីជម្រះអេក្រង់ជាមួយ PowerShell?
Clear-Host
cls # Alias
តើធ្វើដូចម្តេចដើម្បីផ្លាស់ប្តូរគុណភាពបង្ហាញនេះជាមួយ PowerShell?
Set-DisplayResolution -Width 1280 -Height 1024 -Force # Windows 2012
តើធ្វើដូចម្តេចដើម្បីកំណត់ “អេក្រង់ពេញ” លើវីនដូជាមួយ PowerShell?
mode.com 300
តើធ្វើដូចម្តេចដើម្បីទទួលបានការវិមាត្រ (ទទឹងនិងកម្ពស់) នៃរូបភាពជាមួយ PowerShell?
1 2 3 4 5 6 7 |
$picture = New-Object -ComObject Wia.ImageFile $picture.LoadFile('C:\screenshot.jpg') [PSCustomObject] @{ Width = $picture.Width Height = $picture.Height } |
តើធ្វើដូចម្តេចដើម្បីទទួលបានលេខកូដ Windows ជាមួយ 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 43 44 45 |
function Get-WindowsKey { ## function to retrieve the Windows Product Key from any PC ## by Jakob Bindslet (jakob@bindslet.dk) param ($targets = '.') $hklm = 2147483650 $regPath = 'Software\Microsoft\Windows NT\CurrentVersion' $regValue = 'DigitalProductId' Foreach ($target in $targets) { $productKey = $null $win32os = $null $wmi = [WMIClass]"\\$target\root\default:stdRegProv" $data = $wmi.GetBinaryValue($hklm,$regPath,$regValue) $binArray = ($data.uValue)[52..66] $charsArray = 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R', 'T', 'V', 'W', 'X', 'Y', '2', '3', '4', '6', '7', '8', '9' ## decrypt base24 encoded binary data For ($i = 24; $i -ge 0; $i--) { $k = 0 For ($j = 14; $j -ge 0; $j--) { $k = $k * 256 -bxor $binArray[$j] $binArray[$j] = [math]::truncate($k / 24) $k = $k % 24 } $productKey = $charsArray[$k] + $productKey If (($i % 5 -eq 0) -and ($i -ne 0)) { $productKey = '-' + $productKey } } $win32os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $target $obj = New-Object -TypeName Object $obj | Add-Member -MemberType Noteproperty -Name Computer -Value $target $obj | Add-Member -MemberType Noteproperty -Name Caption -Value $win32os.Caption $obj | Add-Member -MemberType Noteproperty -Name CSDVersion -Value $win32os.CSDVersion $obj | Add-Member -MemberType Noteproperty -Name OSArch -Value $win32os.OSArchitecture $obj | Add-Member -MemberType Noteproperty -Name BuildNumber -Value $win32os.BuildNumber $obj | Add-Member -MemberType Noteproperty -Name RegisteredTo -Value $win32os.RegisteredUser $obj | Add-Member -MemberType Noteproperty -Name ProductID -Value $win32os.SerialNumber $obj | Add-Member -MemberType Noteproperty -Name ProductKey -Value $productKey $obj } } |
Perfmon
តើធ្វើដូចម្តេចដើម្បីទទួលបាន “% ពេលដំណើរការ” (មធ្យម) នាពេលថ្មីៗ ក្នុងរយៈពេល 5 វិនាទីចុងក្រោយ (10 ដង) ជាមួយ PowerShell?
(Get-Counter '\Processor(_total)\% Processor Time' -SampleInterval 5 -MaxSamples 10).CounterSamples.CookedValue
Assemblies
តើធ្វើដូចម្តេចដើម្បីផ្ទុកការដំឡើងជាមួយ PowerShell?
1 2 3 4 5 6 |
Add-Type -AssemblyName 'System.Windows.Forms' Add-Type -Path 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll' # Deprecated [System.Reflection.Assembly]::LoadFrom('C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll') |
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលការដំឡើងនាពេលបច្ចុប្បន្នបានផ្ទុកជាមួយ PowerShell?
1 2 3 4 5 |
# Check All [System.AppDomain]::CurrentDomain.GetAssemblies() # Check specific one [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object -FilterScript { $_.FullName -like '*forms*' } |
តើធ្វើដូចម្តេចដើម្បីស្វែងរកផ្លូវ GAC (កន្លែងទុកការដំឡើងសាកល) ជាមួយ PowerShell?
1 |
(New-Object -TypeName Regex -ArgumentList '(?<=file:///)(.*)(?=\/GAC)', 'IgnoreCase').Match(([PSObject].Assembly.Evidence | Where-Object -FilterScript { $_.Value -ne $null }).Value).Value -replace '/', '\' |
Clipboard
តើធ្វើដូចម្តេចដើម្បីចម្លងលទ្ធផលនានាទៅកាន់ក្ដារតម្បៀតខ្ទាស់ជាមួយ PowerShell?
1 |
Get-Process | clip.exe |
តើធ្វើដូចម្តេចដើម្បីទទួលបាននូវមាតិកានៃក្ដារតម្បៀតខ្ទាស់ជាមួយ PowerShell ?
Add-Type -AssemblyName PresentationCore
[Windows.Clipboard]::GetText()
Hotfixes
តើធ្វើដូចម្តេចដើម្បីទទួលបានការដំឡើង hotfixes ជាមួយ PowerShell?
Get-HotFix -ComputerName $computer
តើធ្វើដូចម្តេចដើម្បីទទួលបានការដំឡើង hotfixes មុនពេល/បន្ទាប់ពីកាលបរិច្ឆេទជាក់លាក់មួយដោយ PowerShell?
Get-HotFix | Where-Object -FilterScript { $_.InstalledOn -lt ([DateTime]'01/01/2015') } # Before 01/01/2015
Get-HotFix | Where-Object -FilterScript {$_.InstalledOn -gt ([DateTime]'01/01/2015')} # After 01/01/2015
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលប្រសិនបើ hotfix មួយដែលត្រូវបានដំឡើងដោយ PowerShell?
Get-HotFix -Id KB2965142
តើធ្វើដូចម្តេចដើម្បីទទួលបានការដំឡើង hotfixes នៅលើកុំព្យូទ័រពីចម្ងាយជាមួយ PowerShell?
Get-HotFix -ComputerName $computer
Pagefile
តើធ្វើដូចម្តេចដើម្បីទទួលបានព័ត៌មានចំលាស់ជាមួយ PowerShell?
Get-WmiObject -Class Win32_PageFileusage | Select-Object -Property Name, CurrentUsage, AllocatedBaseSize, PeakUsage, InstallDate
តើធ្វើដូចម្តេចដើម្បីទទួលបានទំហំដែលបានផ្ដល់អនុសាសន៍ (MB) សម្រាប់ចំលាស់ការជាមួយ PowerShell នេះ?
[Math]::Truncate(((Get-WmiObject -Class Win32_ComputerSystem).TotalPhysicalMemory) / 1MB) * 1.5
តើធ្វើដូចម្តេចដើម្បីបង្កើតចំលាស់ (4096 មេកាបៃ) មួយនៅលើ ដ្រាយ (D 🙂 ជាមួយ PowerShell?
1 2 3 4 5 |
Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{ Name = 'D:\pagefile.sys' InitialSize = 4096 MaximumSize = 4096 } |
តើធ្វើដូចម្តេចដើម្បីលុបចំលាស់មួយនៅលើ ដ្រាយ (C 🙂 ជាមួយ PowerShell?
1 2 3 4 5 |
$privileges = Get-WmiObject -Class Win32_computersystem -EnableAllPrivileges $privileges.AutomaticManagedPagefile = $false $privileges.Put() $pagefile = Get-WmiObject -Query "select * from Win32_PageFileSetting where name='c:\\pagefile.sys'" $pagefile.Delete() # Reboot required |
Maintenance
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលការបែងចែកនៃដ្រាយជាមួយ PowerShell?
1 |
$drive = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = 'c:'" $defragReport = $drive.DefragAnalysis() $defragReport.DefragAnalysis |
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលទំហំថាសរបស់ដ្រាយជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Get-WmiObject -Class Win32_logicaldisk | Format-Table -Property @{ Name = 'Drive' Expression = {$_.DeviceID} }, @{ Name = 'Total size (GB)' Expression = {[decimal]('{0:N0}' -f($_.Size/1gb))} }, @{ Name = 'Free space(GB)' Expression = {[decimal]('{0:N0}'-f($_.Freespace/1gb))} }, @{ Name = 'Free (%)' Expression = {'{0,6:P0}' -f(($_.Freespace/1gb) / ($_.size/1gb))} } -AutoSize |
Files
តើធ្វើដូចម្តេចដើម្បីបើកឯកសារជាមួយ PowerShell?
Invoke-Item -Path 'C:\scripts\file.txt'
.'C:\scripts\file.txt'
តើធ្វើដូចម្តេចដើម្បីអានឯកសារជាមួយ PowerShell?
Get-Content -Path 'C:\scripts\file.txt'
gc "C:\scripts\file.txt" # Alias
របៀបសរសេរបញ្ចេញទៅឯកសារជាមួយ PowerShell?
'Line1', 'Line2', 'Line3' | Out-File -FilePath 'C:\scripts\file.txt'
'Line1', 'Line2', 'Line3' | Add-Content -Path file.txt
តើធ្វើដូចម្តេចដើម្បីទទួលបានឈ្មោះពេញរបស់ឯកសារបច្ចុប្បន្នជាមួយស្គ្រីប PowerShell?
$MyInvocation.MyCommand.Path
តើធ្វើដូចម្តេចដើម្បីបង្ហាប់/ឯកសារដែលបានបង្ហាប់ជាមួយនឹងការ PowerShell?
Add-Type -AssemblyName 'System.IO.Compression.Filesystem'
[System.IO.Compression.ZipFile]::CreateFromDirectory($folder,$fileZIP)
តើធ្វើដូចម្តេចដើម្បីពន្លាឯកសារ/ពន្លាការជាមួយ PowerShell?
Add-Type -AssemblyName 'System.IO.Compression.Filesystem'
[System.IO.Compression.ZipFile]::ExtractToDirectory($fileZIP, $folder)
តើធ្វើដូចម្តេចដើម្បីមើលឃើញឯកសារក្នុងប័ណ្ណសារ ZIP មួយជាមួយ PowerShell?
Add-Type -AssemblyName 'System.IO.Compression.Filesystem'
[System.IO.Compression.ZipFile]::OpenRead($fileZIP)
តើធ្វើដូចម្តេចដើម្បីបង្ហាញទំហំឯកសារមួយនៅក្នុងគីឡូបៃការជាមួយ PowerShell?
(Get-ChildItem -Path .\winsrv.dll).Length /1KB
(Get-ChildItem -Path .\winsrv.dll).Length /1MB
(Get-ChildItem -Path .\winsrv.dll).Length /1GB
តើធ្វើដូចម្តេចដើម្បីស្វែងរកឯកសារដែលមានទំហំធំឬតិចជាង 1 ជីកាបៃជាមួយ PowerShell?
1 2 3 4 5 |
# Larger than 1 GB Get-ChildItem -Path C:\ -Recurse -ErrorVariable $errorsSearch | Where-Object -FilterScript {$_.Length -gt 1GB} # Less than 1 GB Get-ChildItem -Path C:\ -Recurse -ErrorVariable $errorsSearch | Where-Object -FilterScript {$_.Length -lt 1GB} |
តើធ្វើដូចម្តេចដើម្បីបង្ហាញឈ្មោះឯកសារដោយមិនបានទូលំទូលាយជាមួយ PowerShell?
[System.IO.Path]::GetFileNameWithoutExtension('C:\Windows\system32\calc.exe') # Return calc
តើធ្វើដូចម្តេចដើម្បីបង្ហាញផ្នែកបន្ថែមនៃឯកសារជាមួយ PowerShell?
[System.IO.Path]::GetExtension('C:\scripts\file.txt') # Return .txt
តើធ្វើដូចម្តេចដើម្បីទទួលបានកំណែឯកសារនៃឯកសារជាមួយ PowerShell?
1 2 |
(Get-Item -Path C:\Windows\System32\calc.exe).VersionInfo.FileVersion [System.Diagnostics.FileVersionInfo]::GetVersionInfo('C:\Windows\system32\calc.exe').FileVersion |
តើធ្វើដូចម្តេចដើម្បីទទួលបានសញ្ញានៃឯកសារជាមួយ PowerShell?
(Get-FileHash $file).Hash
តើធ្វើដូចម្តេចដើម្បីទទួលបានឆេកសាំ MD5/SHA1 នៃឯកសារជាមួយ PowerShell?
Get-FileHash $file -Algorithm MD5
Get-FileHash $file -Algorithm SHA1
តើធ្វើដូចម្តេចដើម្បីបង្ហាញឯកសារដែលបានលាក់ជាមួយ PowerShell?
1 2 3 4 5 |
# Display only hidden files Get-ChildItem -Hidden -File # Display all files (including hidden files) Get-ChildItem -Force -File |
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលប្រសិនបើមានឯកសារមួយមានផ្នែកបន្ថែមជាមួយ PowerShell?
1 |
[System.IO.Path]::HasExtension('C:\hiberfil.sys') |
របៀបកំណត់ឯកសារដូចជា “បានតែអាន” ជាមួយ PowerShell?
Set-ItemProperty -Path .\file.txt -Name IsReadOnly -Value $true
តើធ្វើដូចម្តេចដើម្បីផ្លាស់ប្តូរ “LastWriteTime” គុណលក្ខណៈដើម្បីសប្តាហ៍មុនសម្រាប់ឯកសារជាមួយ PowerShell?
Set-ItemProperty -Path .\file.txt -Name LastWriteTime -Value ((Get-Date).AddDays(-7))
If not working, use Nirsoft tool: BulkFileChanger.
តើធ្វើដូចម្តេចដើម្បីបង្កើតឯកសារថ្មីមួយជាមួយ PowerShell?
New-Item -ItemType File -Path 'C:\scripts\file.txt' -Value 'FirstLine'
តើធ្វើដូចម្តេចដើម្បីប្តូរឈ្មោះឯកសារដែលមានជាមួយ PowerShell?
Rename-Item -Path 'C:\scripts\file.txt' -NewName 'C:\scripts\powershellguru2.txt'
តើធ្វើដូចម្តេចដើម្បីកែឈ្មោះទំហំ/ចំនួនច្រើនដែលមានឯកសារច្រើនជាមួយ PowerShell?
Get-ChildItem -Path C:\scripts\txt | Rename-Item -NewName { $_.Name -replace ' ', '_' }
របៀបលុបឯកសារជាមួយ PowerShell?
Remove-Item -Path 'C:\scripts\file.txt'
តើធ្វើដូចម្តេចដើម្បីបង្ហាញបន្ទាត់ថ្មីបំផុតនៃឯកសារដែលបានចំនួន 10 ដោយមាន PowerShell?
Get-Content -Path 'C:\scripts\log.txt' -Tail 10
តើធ្វើដូចម្តេចដើម្បីធ្វើការបើកឯកសារដែលមានច្រើននៃថតជាមួយ PowerShell?
Get-ChildItem -Path 'C:\scripts\Modules' | Unblock-File
តើធ្វើដូចម្តេចដើម្បីយកបន្ទាត់ទទេពីឯកសារជាមួយ PowerShell?
(Get-Content -Path file.txt) | Where-Object -FilterScript {$_.Trim() -ne '' } | Set-Content -Path file.txt
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលប្រសិនបើមានឯកសារមួយជាមួយ PowerShell?
1 |
Test-Path -Path 'C:\Windows\notepad.exe' # Return True |
តើធ្វើដូចម្តេចដើម្បីទទួលបានឯកសារថ្មីបំផុត/ចំណាស់ជាងគេដែលបានបង្កើតនៅក្នុងថតដែលមាន PowerShell?
1 2 |
Get-ChildItem | Sort-Object -Property CreationTime | Select-Object -Last 1 # Newest Get-ChildItem | Sort-Object -Property CreationTime | Select-Object -First 1 # Oldest |
តើធ្វើដូចម្តេចដើម្បីយកបន្ទាត់ស្ទួនពីឯកសារជាមួយ PowerShell?
1 2 |
Get-Content -Path .\file.txt | Select-Object -Unique # Display Get-Content -Path .\file.txt | Select-Object -Unique | Set-Content -Path .\testing.txt # Save |
តើធ្វើដូចម្តេចដើម្បីទទួលបានឯកសារដែលបានបង្កើតឡើងច្រើនឬតិចជាង 1 ខែនៅក្នុងថតជាមួយ PowerShell?
1 2 3 |
$1MonthAgo = (Get-Date).AddMonths(-1) Get-ChildItem | ?{$_.LastWriteTime -lt $1MonthAgo} | Select-Object LastWriteTime,Name,DirectoryName # More Get-ChildItem | ?{$_.LastWriteTime -gt $1MonthAgo} | Select-Object LastWriteTime,Name,DirectoryName # Less |
តើធ្វើដូចម្តេចដើម្បីទទួលបានឯកសារដែលបានបង្កើតឡើងច្រើនឬតិចជាង 1 ឆ្នាំនៅក្នុងការថតជាមួយ PowerShell?
1 2 3 |
$1YearAgo = (Get-Date).AddYears(-1) Get-ChildItem | ?{$_.LastWriteTime -lt $1YearAgo} | Select-Object LastWriteTime,Name,DirectoryName # More Get-ChildItem | ?{$_.LastWriteTime -gt $1YearAgo} | Select-Object LastWriteTime,Name,DirectoryName # Less |
តើធ្វើដូចម្តេចដើម្បីនាំចេញតម្លៃនៃអថេរមួយទៅកាន់ឯកសារជាមួយ PowerShell?
Set-Content -Path file.txt -Value $variable
តើធ្វើដូចម្តេចដើម្បីរាប់ចំនួននៃឯកសារ (* .txt) នៅក្នុងថតដោយមាន PowerShell?
1 2 3 |
[System.IO.Directory]::GetFiles('C:\scripts', '*.txt').Count (Get-ChildItem -Path 'C:\scripts' -Filter *.txt).Count (Get-ChildItem -Path 'C:\scripts' -Filter *.txt -Recurse).Count # Recursive |
តើធ្វើដូចម្តេចដើម្បីស្វែងរកខ្សែអក្សរនៅក្នុងឯកសារជាច្រើនជាមួយ PowerShell?
Select-String -Path 'C:\*.txt' -Pattern 'Test'
តើធ្វើដូចម្តេចដើម្បីបង្ហាញជាលើកដំបូង/បន្ទាត់ចុងក្រោយនៃឯកសារដោយប្រើ PowerShell?
1 2 3 4 5 6 7 8 9 |
'Line1', 'Line2', 'Line3' | Out-File -FilePath file.txt # First Line Get-Content -Path .\file.txt | Select-Object -First 1 # Returns Line1 (Get-Content -Path .\file.txt)[0] # Returns Line1 # Last Line Get-Content -Path .\file.txt | Select-Object -Last 1 # Returns Line3 (Get-Content -Path .\file.txt)[-1] # Returns Line3 |
តើធ្វើដូចម្តេចដើម្បីបង្ហាញលេខបន្ទាត់ជាក់លាក់របស់ឯកសារជាមួយ PowerShell?
1 2 3 |
'Line1', 'Line2', 'Line3' | Out-File -FilePath file.txt Get-Content -Path .\file.txt | Select-Object -Index 0 # Returns Line1 Get-Content -Path .\file.txt | Select-Object -Index 2 # Returns Line3 |
តើធ្វើដូចម្តេចដើម្បីរាប់ចំនួនបន្ទាត់នៃឯកសារជាមួយ PowerShell?
1 2 |
'Line1', 'Line2', 'Line3' | Out-File -FilePath file.txt (Get-Content -Path .\file.txt | Measure-Object -Line).Lines # Returns 3 |
តើធ្វើដូចម្តេចដើម្បីរាប់ចំនួននៃតួអក្សរនិងពាក្យនៃឯកសារជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 10 |
'Test', 'Powershell', 'Test Powershell' | Out-File -FilePath file.txt # Words (Return 4) (Get-Content -Path .\file.txt | Measure-Object -Word).Words # Characters (Return 23) (Get-Content -Path .\file.txt | Measure-Object -Character).Characters # Characters and ignore whitespaces (Return 22) (Get-Content -Path .\file.txt | Measure-Object -Character -IgnoreWhiteSpace).Characters |
របៀបទាញយកឯកសារជាមួយ PowerShell?
Invoke-WebRequest -Uri 'http://www.nirsoft.net/utils/searchmyfiles.zip' -OutFile 'C:\tools\searchmyfiles.zip'
តើធ្វើដូចម្តេចដើម្បីបង្ហាញផ្លូវពេញលេញនៃឯកសារជាមួយ PowerShell?
Resolve-Path -Path .\script.ps1 # Return C:\Scripts\script.ps1
Copy
តើធ្វើដូចម្តេចដើម្បីចម្លងឯកសារមួយទៅថតជាមួយ PowerShell?
Copy-Item -Path 'C:\source\file.txt' -Destination 'C:\destination'
តើធ្វើដូចម្តេចដើម្បីចម្លងឯកសារមួយទៅថតជាច្រើនជាមួយ PowerShell?
1 2 |
$destination = 'C:\destination\Folder1', 'C:\destination\Folder2' $destination | Copy-Item -Path 'C:\source\file.txt' -Recurse -Destination {$_} |
តើធ្វើដូចម្តេចដើម្បីចម្លងឯកសារច្រើនទៅក្នុងថតមួយជាមួយ PowerShell?
Get-ChildItem -Path 'C:\source' -Filter *.txt | Copy-Item -Destination 'C:\destination'
Active Directory
Domain & Forest
តើធ្វើដូចម្តេចដើម្បីស្វែងរកម៉ាស៊ីនបម្រើកាតាឡុកពិភពលោកក្នុងថតសកម្មជាមួយ PowerShell?
[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().GlobalCatalogs
តើធ្វើដូចម្តេចដើម្បីរកកន្លែងក្នុងថតសកម្មជាមួយ PowerShell?
[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites
តើធ្វើដូចម្តេចដើម្បីស្វែងរកការឧបករណ៍បញ្ជាដែនបច្ចុប្បន្នជាមួយ PowerShell?
(Get-ADDomainController).HostName
តើធ្វើដូចម្តេចដើម្បីស្វែងរកឧបករណ៍បញ្ជាដែនទាំងអស់នៅក្នុងដែនជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 10 11 |
# Solution 1 Get-ADDomainController -Filter * | ForEach-Object -Process {$_.Name} # Solution 2 Get-ADGroupMember 'Domain Controllers' | ForEach-Object -Process {$_.Name} # Solution 3 Get-ADComputer -LDAPFilter '(&(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=8192))' | ForEach-Object -Process {$_.Name} # Solution 4 [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() | ForEach-Object -Process {$_.DomainControllers} | ForEach-Object -Process {$_.Name} |
តើធ្វើដូចម្តេចដើម្បីស្វែងរកការថតចម្លង AD មិនបានសម្រេចជាមួយ PowerShell?
Get-ADReplicationFailure dc02.domain.com # Windows 8 and 2012
1 |
(Get-ADObject -Identity "cn=Directory Service,cn=Windows NT,cn=Services,$(([adsi]('LDAP://RootDSE')).configurationNamingContext)" -Properties tombstonelifetime).tombstonelifetime |
តើធ្វើដូចម្តេចដើម្បីទទួលបានពត៌មានលម្អិតនៃព្រៃ/ដែននៅក្នុងថតសកម្មជាមួយ PowerShell?
1 2 |
Get-ADDomain domain.com Get-ADForest domain.com |
តើធ្វើដូចម្តេចដើម្បីទទួលបានផ្លូវនៃ “វត្ថុដែលបានលុប” ធុងក្នុងថតសកម្មជាមួយ PowerShell?
(Get-ADDomain).DeletedObjectsContainer
តើធ្វើដូចម្តេចដើម្បីបើកលក្ខណៈពិសេសនៃធុងសម្រាម AD ក្នុងការថតសកម្មជាមួយ PowerShell?
1 |
Enable-ADOptionalFeature -Identity 'CN=Recycle Bin Feature,CN=Optional Features,CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,DC=domain,DC=com' -Scope ForestOrConfigurationSet -Target 'domain.com' |
តើធ្វើដូចម្តេចដើម្បីស្ដារឡើងវិញនូវគណនី AD ពីធុងសំរាមនៅក្នុងថតសកម្មជាមួយ PowerShell?
Get-ADObject -Filter 'samaccountname -eq "powershellguru"' -IncludeDeletedObjects | Restore-ADObject
តើធ្វើដូចម្តេចដើម្បីស្វែងរកតួនាទី FSMO ជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Solution 1 Get-ADForest | Format-List -Property SchemaMaster, DomainNamingMaster Get-ADDomain | Format-List -Property PDCEmulator, RIDMaster, InfrastructureMaster # Solution 2 netdom query fsmo # Solution 3 [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().SchemaRoleOwner [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().NamingRoleOwner [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().InfrastructureRoleOwner [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().PdcRoleOwner [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().RidRoleOwner |
តើធ្វើដូចម្តេចដើម្បីភ្ជាប់ទៅកាន់ឧបករណ៍បញ្ជាដែនជាក់លាក់ជាមួយ PowerShell?
Get-ADUser -Identity $user -Server 'serverDC01'
តើធ្វើដូចម្តេចដើម្បីទទួលបានម៉ាស៊ីនបម្រើចូលបច្ចុប្បន្នជាមួយ PowerShell?
1 2 |
($env:LOGONSERVER).Substring(2) ([System.Environment]::GetEnvironmentVariable('logonserver')).Substring(2) |
តើធ្វើដូចម្តេចដើម្បីអនុវត្ត “gpupdate” នៅលើកុំព្យូទ័រជាមួយ PowerShell?
Invoke-GPUpdate -Computer $computer -Force -RandomDelayInMinutes 0 # Windows 2012
Groups
តើធ្វើដូចម្តេចដើម្បីបង្កើតក្រុមថ្មីនៅក្នុងថតសកម្មជាមួយ PowerShell?
1 |
New-ADGroup -Name 'Powershell Guru' -SamAccountName powershellguru -GroupCategory Security -GroupScope Global -DisplayName 'Powershell Guru' -Path 'OU=MyOU,DC=domain,DC=com' -Description 'My account' |
តើធ្វើដូចម្តេចដើម្បីយកចេញជាក្រុមមួយនៅក្នុងថតសកម្មជាមួយ PowerShell?
Remove-ADGroup -Identity 'PowershellGuru'
តើធ្វើដូចម្តេចដើម្បីបន្ថែមអ្នកប្រើទៅក្រុមមួយនៅក្នុងថតសកម្មជាមួយ PowerShell?
Add-ADGroupMember "Powershell Guru" -Members powershellguru
តើធ្វើដូចម្តេចដើម្បីយកអ្នកប្រើចេញពីក្រុមមួយនៅក្នុងថតសកម្មជាមួយ PowerShell?
Remove-ADGroupMember 'Powershell Guru' -Members powershellguru
តើធ្វើដូចម្តេចដើម្បីស្វែងរកក្រុមទទេ (គ្មានសមាជិក) នៅក្នុងថតសកម្មជាមួយ PowerShell?
Get-ADGroup -Filter * -Properties Members | Where-Object -FilterScript {-not $_.Members}
តើធ្វើដូចម្តេចដើម្បីរាប់ក្រុមទទេ (គ្មានសមាជិក) នៅក្នុងថតសកម្មជាមួយ PowerShell?
(Get-ADGroup -Filter * -Properties Members | Where-Object -FilterScript {-not $_.Members}).Count
តើធ្វើដូចម្តេចដើម្បីទទួលបានសមាជិករបស់ក្រុមមួយនៅក្នុងថតសកម្មជាមួយ 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 |
# Solution 1 Get-ADGroupMember 'Powershell Guru' | ForEach-Object -Process {$_.DistinguishedName} Get-ADGroupMember 'Powershell Guru' | ForEach-Object -Process {$_.Samaccountname} # Solution 2 Get-ADGroup 'Powershell Guru' -Properties Members | Select-Object -Property Members -ExpandProperty Members | Sort-Object # Solution 3 function Get-ADGroupMemberFast { [CmdletBinding()] Param ( [Parameter(Mandatory = $true)] [string]$GroupName ) $de = New-Object -TypeName System.DirectoryServices.DirectoryEntry $ds = New-Object -TypeName System.DirectoryServices.DirectorySearcher $ds.SearchRoot = $de $ds.Filter = "(cn=$group)" $null = $ds.PropertiesToLoad.Add('member') $result = $ds.FindOne() if($result) { $account = $result.GetDirectoryEntry() $account.Properties['member'] | ForEach-Object -Process {$_} } } Get-ADGroupMemberFast -GroupName 'Powershell Guru' |
1 2 |
Get-ADGroupMember 'Powershell Guru' -Recursive | ForEach-Object -Process {$_.DistinguishedName} Get-ADGroupMember 'Powershell Guru' -Recursive | ForEach-Object -Process {$_.SamAccountName} |
1 2 |
(Get-ADGroupMember 'Powershell Guru' | ForEach-Object -Process {$_.Samaccountname}).Count (Get-ADGroupMember 'Powershell Guru' -Recursive | ForEach-Object -Process {$_.Samaccountname}).Count |
Users
តើធ្វើដូចម្តេចដើម្បីប្រើតួអក្សរជំនួសក្នុងតម្រងរបស់ “ទទួលបាន-ADUser” នៅក្នុងថតសកម្មជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 |
# Filter (Get-ADUser -SearchBase 'OU=myOU,DC=domain,DC=com' -Filter {name -like '*vip*'} -Properties Name).Name # LDAPFilter (Get-ADUser -SearchBase 'OU=myOU,DC=domain,DC=com' -LDAPFilter '(name=*vip*)' -Properties Name).Name # With a variable $user = '*vip*' (Get-ADUser -SearchBase 'OU=myOU,DC=domain,DC=com' -Filter {name -like $user} -Properties Name).Name |
តើធ្វើដូចម្តេចដើម្បីផ្លាស់ប្តូរអ្នកប្រើ OU ផ្សេងទៀតនៅក្នុងថតសកម្មជាមួយ PowerShell?
Move-ADObject -Identity $dn -TargetPath 'OU=myOU,DC=domain,DC=com'
តើធ្វើដូចម្តេចដើម្បីស្វែងរកសមាជិកទាំងអស់ដែលមាន (Nested) សម្រាប់អ្នកប្រើជាមួយ PowerShell?
Get-ADGroup -LDAPFilter "(member:1.2.840.113556.1.4.1941:=$($dn))"
តើធ្វើដូចម្តេចដើម្បីទទួលបានសមាជិក (ឈ្មោះខ្លី/កាត់ឱ្យខ្លី) សម្រាប់អ្នកប្រើដែលមាន PowerShell?
(Get-ADUser $user -Properties MemberOf).MemberOf | ForEach-Object -Process {($_ -split ',')[0].Substring(3)} | Sort-Object
1 2 |
Set-ADUser $samAccountName -DisplayName 'DisplayName' -GivenName 'Test' -Surname 'Powershell' -DisplayName 'Test Powershell' Rename-ADObject $dn -NewName 'Test Powershell' #FullName |
តើធ្វើដូចម្តេចដើម្បីផ្លាស់ប្តូរទិសដៅ ការិយាល័យនិងលេខទូរស័ព្ទសម្រាប់គណនីអ្នកប្រើនៅក្នុងថតសកម្មដោយមាន PowerShell?
Set-ADUser $samAccountName -Description 'IT Consultant' -Office 'Building B' -OfficePhone '12345'
1 2 3 4 5 |
# 31/12/2015 Set-ADAccountExpiration $samAccountName -DateTime '01/01/2016' # Never Clear-ADAccountExpiration $samAccountName |
តើធ្វើដូចម្តេចដើម្បីដោះសោគណនីអ្នកប្រើនៅក្នុងថតសកម្មដោយមាន PowerShell?
Unlock-ADAccount $samAccountName
តើធ្វើដូចម្តេចដើម្បីបើក/បិទគណនីអ្នកប្រើនៅក្នុងថតសកម្មដោយមាន PowerShell?
1 2 |
Disable-ADAccount $samAccountName Enable-ADAccount $samAccountName |
តើធ្វើដូចម្តេចដើម្បីយកគណនីអ្នកប្រើនៅក្នុងថតសកម្មដោយមាន PowerShell?
Remove-ADUser $samAccountName
តើធ្វើដូចម្តេចដើម្បីកំណត់ពាក្យសម្ងាត់សម្រាប់គណនីអ្នកប្រើម្នាក់មួយនៅក្នុងថតសកម្មដោយមាន PowerShell?
1 2 3 4 5 6 7 |
# Solution 1 : ask password $password = Read-Host -Prompt 'New Password' -AsSecureString # Solution 2 : specify password $password = ConvertTo-SecureString -String 'Q>9xYMw<3?' -AsPlainText -Force Get-ADUser -Filter "samaccountname -like 'helpdeskagent*'" | Set-ADAccountPassword -NewPassword $newpwd -Reset -PassThru | Set-ADuser -ChangePasswordAtLogon $true |
1 2 3 4 5 6 7 |
# Solution 1 : ask password $password = Read-Host -Prompt 'New Password' -AsSecureString # Solution 2 : specify password $password = ConvertTo-SecureString -String 'Q>9xYMw<3?' -AsPlainText -Force Get-ADUser -Filter "samaccountname -like 'helpdeskagent*'" | Set-ADAccountPassword -NewPassword $newpwd -Reset -PassThru | Set-ADuser -ChangePasswordAtLogon $true |
តើធ្វើដូចម្តេចដើម្បីស្វែងរកម្ចាស់ឯកសារនៅក្នុងថតសកម្មដោយមាន PowerShell ?
1 2 3 |
$user = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList (Get-Acl -Path 'userFile.txt').Owner $sid = $user.Translate([System.Security.Principal.SecurityIdentifier]).Value Get-ADUser $sid |
តើធ្វើដូចម្តេចដើម្បីស្វែងរក OU (អង្គភាពស្ថាប័ន) សម្រាប់អ្នកប្រើមួយនៅក្នុងថតសកម្មដោយមាន PowerShell?
[regex]::match("$((Get-ADUser $user -Properties DistinguishedName).DistinguishedName)",'(?=OU=)(.*\n?)').value
តើធ្វើដូចម្តេចដើម្បីស្វែងរកគណនីអ្នកប្រើជនពិការនៅក្នុងថតសកម្មដោយមាន PowerShell?
1 2 |
Search-ADAccount -AccountDisabled Get-ADUser -Filter {Enabled -ne $true} |
តើធ្វើដូចម្តេចដើម្បីស្វែងរកគណនីអ្នកប្រើបានផុតកំណត់នៅក្នុងថតសកម្មដោយមាន PowerShell?
Search-ADAccount -AccountExpired
តើធ្វើដូចម្តេចដើម្បីស្វែងរកគណនីអ្នកប្រើចាក់សោក្នុងថតសកម្មដោយមាន PowerShell?
Search-ADAccount -LockedOut
តើធ្វើដូចម្តេចដើម្បីស្វែងរក SID នៃគណនីអ្នកប្រើនៅក្នុងថតសកម្មដោយមាន PowerShell?
(Get-ADUser $user -Properties SID).SID.Value
តើធ្វើដូចម្តេចដើម្បីបម្លែងឈ្មោះអ្នកប្រើដើម្បី SID ក្នុងថតសកម្មដោយមាន PowerShell?
1 2 |
$user = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList ('DOMAIN', 'user') $SID = ($user.Translate([System.Security.Principal.SecurityIdentifier])).Value |
តើធ្វើដូចម្តេចដើម្បីបម្លែង SID មួយដើម្បីប្រើឈ្មោះអ្នកប្រើនៅក្នុងថតសកម្មដោយមាន PowerShell?
1 2 |
$SID = New-Object -TypeName System.Security.Principal.SecurityIdentifier -ArgumentList ('SID') $user = ($SID.Translate( [System.Security.Principal.NTAccount])).Value |
តើធ្វើដូចម្តេចដើម្បីបំបែកឈ្មោះកិត្តិយសនៃគណនីអ្នកប្រើនៅក្នុងថតសកម្មដោយមាន PowerShell?
1 2 3 |
$dn = 'CN=Powershell Test,OU=TEST,DC=domain,DC=com' $dn.Split(',')[0] # Returns "CN=Powershell Test" $dn.Split(',')[0].Split('=')[1] # Returns "Powershell Test" |
តើធ្វើដូចម្តេចដើម្បីស្វែងរកកាលបរិច្ឆេទនៃការបង្កើត/កែប្រែគណនីអ្នកប្រើនៅក្នុងថតសកម្មដោយមាន PowerShell?
Get-ADUser -Identity $user -Properties whenChanged, whenCreated | Format-List -Property whenChanged, whenCreated
1 2 3 |
$schema = [DirectoryServices.ActiveDirectory.ActiveDirectorySchema]::GetCurrentSchema() $schema.FindClass('user').mandatoryproperties | Format-Table $schema.FindClass('user').optionalproperties | Format-Table |
តើធ្វើដូចម្តេចដើម្បីទទួលបានផ្លូវបម្រើ LDAP សម្រាប់អ្នកប្រើមួយនៅក្នុងការថតសកម្មដោយមាន PowerShell?
1 2 3 4 |
$searcher = New-Object -TypeName DirectoryServices.DirectorySearcher -ArgumentList ([ADSI]'') $searcher.Filter = "(&(objectClass=user)(sAMAccountName= $user))" $searcher = $searcher.FindOne() $pathLDAP = $searcher.Path |
តើធ្វើដូចម្តេចដើម្បីផ្លាស់ប្តូរ CN (ដោយ Canonical ឈ្មោះ) សម្រាប់អ្នកប្រើមួយនៅក្នុងថតសកម្មដោយមាន PowerShell?
Rename-ADObject $((Get-ADUser $user -Properties DistinguishedName).DistinguishedName) -NewName 'Test Powershell'
1 2 |
$dn = (Get-ADUser $user -Properties DistinguishedName).DistinguishedName $parent = $dn.Split(',',2)[1] |
តើធ្វើដូចម្តេចដើម្បីទទួលបានជាម្ចាស់អ្នកប្រើ (ដែលបានបង្កើតគណនី) នៅក្នុងថតសកម្មដោយមាន PowerShell នេះ?
1 2 |
$dn = (Get-ADUser $user -Properties DistinguishedName).DistinguishedName $owner = (Get-Acl -Path "AD:$dn").Owner |
តើធ្វើដូចម្តេចដើម្បីបម្លែងគុណលក្ខណៈ PwdLastSet សម្រាប់អ្នកប្រើនៅក្នុងថតសកម្មដោយមាន PowerShell?
1 2 3 4 5 |
# Solution 1 [DateTime]::FromFileTime((Get-ADUser $user -Properties pwdLastSet).pwdLastSet) # Solution 2 w32tm /ntte 130787549514737594 |
Computers
តើធ្វើដូចម្តេចដើម្បីសាកល្បងឆានែលដែលមានសុវត្ថិភាពរវាងកុំព្យូទ័រមូលដ្ឋាននិងដែនជាមួយ PowerShell ?
Test-ComputerSecureChannel
តើធ្វើដូចម្តេចដើម្បីជួសជុលបណ្តាញដែលមានសុវត្ថិភាពរវាងកុំព្យូទ័រមូលដ្ឋាននិងដែនជាមួយ PowerShell ?
Test-ComputerSecureChannel -Repair
តើធ្វើដូចម្តេចដើម្បីបិទកុំព្យូទ័រនៅក្នុងគណនីថតសកម្មដោយមាន PowerShell?
Disable-ADAccount $computer
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 |
Get-ADComputer -Filter 'OperatingSystem -eq "CentOS"' Get-ADComputer -Filter 'OperatingSystem -eq "GNU/Linux"' Get-ADComputer -Filter 'OperatingSystem -eq "Linux"' Get-ADComputer -Filter 'OperatingSystem -eq "Mac OS X"' Get-ADComputer -Filter 'OperatingSystem -eq "OnTap"' Get-ADComputer -Filter 'OperatingSystem -eq "Red Hat Enterprise Linux Server"' Get-ADComputer -Filter 'OperatingSystem -eq "redhat-linux-gnu"' Get-ADComputer -Filter 'OperatingSystem -eq "Samba"' Get-ADComputer -Filter 'OperatingSystem -eq "Ubuntu"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows NT"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows 2000 Professional"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows 2000 Server"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows XP Professional"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows Server 2003"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows Vista™ Business"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows Vista™ Enterprise"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows Vista™ Entreprise"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows 7 Enterprise"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows 7 Professional"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows 7 Ultimate"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows Server 2008 R2 Enterprise"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows Server 2008 R2 Standard"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows Server® 2008 Enterprise"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows 8 Enterprise"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows 8.1 Enterprise"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows Server 2012 R2 Standard"' Get-ADComputer -Filter 'OperatingSystem -eq "Windows Server 2012 Standard"' |
Organizational Unit (OU)
តើធ្វើដូចម្តេចដើម្បីបង្កើតអង្គភាពស្ថាប័ន (OU) នៅក្នុងថតសកម្មដោយមាន PowerShell?
New-ADOrganizationalUnit -Name 'TEST' -Path 'DC=domain,DC=com'
តើធ្វើដូចម្តេចដើម្បីទទួលបានអង្គភាពស្ថាប័ន (OU) សេចក្ដីលម្អិតនៅក្នុងថតសកម្មដោយមាន PowerShell?
Get-ADOrganizationalUnit 'OU=TEST,DC=domain,DC=com' -Properties *
តើធ្វើដូចម្តេចដើម្បីផ្លាស់ប្តូរការពិពណ៌នាមួយនៃអង្គភាពស្ថាប័ន (OU) ក្នុងថតសកម្មដោយមាន PowerShell?
Set-ADOrganizationalUnit 'OU=TEST,DC=domain,DC=com' -Description 'My description'
1 2 3 4 5 |
# Protection ON Set-ADOrganizationalUnit 'OU=TEST,DC=domain,DC=com' -ProtectedFromAccidentalDeletion $true # Protection OFF Set-ADOrganizationalUnit 'OU=TEST,DC=domain,DC=com' -ProtectedFromAccidentalDeletion $false |
1 |
Get-ADOrganizationalUnit -Filter * -Property ProtectedFromAccidentalDeletion | Where-Object -FilterScript { $_.ProtectedFromAccidentalDeletion -eq $false } | Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $true |
តើធ្វើដូចម្តេចដើម្បីលុបអង្គភាពស្ថាប័ន (OU) ការពារពីការលុបដោយចៃដន្យនៅក្នុងថតសកម្មដោយមាន PowerShell?
1 2 |
Set-ADOrganizationalUnit 'OU=TEST,DC=domain,DC=com' -ProtectedFromAccidentalDeletion $false Remove-ADOrganizationalUnit 'OU=TEST,DC=domain,DC=com' |
1 2 |
$parent = $dn.Split(',',2)[1] $parent = (Get-ADOrganizationalUnit $parent -Properties CanonicalName).CanonicalName |
តើធ្វើដូចម្តេចដើម្បីរាយអង្គភាពស្ថាប័នទទេ (OUs) ជាមួយ PowerShell?
1 2 3 4 5 |
# Solution 1 Get-ADOrganizationalUnit -Filter * -Property 'msDS-Approx-Immed-Subordinates' | Where-Object -FilterScript {$_.'msDS-Approx-Immed-Subordinates' -eq 0} # Solution 2 ([adsisearcher]'(objectclass=organizationalunit)').FindAll() | Where-Object -FilterScript { (([adsi]$_.Path).PSbase.Children | Measure-Object).Count -eq 0 } |
តើធ្វើដូចម្តេចដើម្បីទទួលបានការគ្រប់គ្រងនៃក្រុមមួយ PowerShell?
(Get-ADGroup $dn -Properties Managedby).Managedby
Regex (Regular Expression)
តើធ្វើដូចម្តេចដើម្បីទាញយកជាមួយ V4 អាសយដ្ឋាន IP (80.80.228.8) ជាមួយ regex ជាមួយ PowerShell?
$example = 'The IP address is 80.80.228.8'
$ip = [regex]::match($example,'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b').value
តើធ្វើដូចម្តេចដើម្បីទាញយកអាសយដ្ឋាន MAC (C0-D9-62-39-61-2D) ដែលមានសញ្ញាបំបែក “-” ជាមួយ regex ជាមួយ PowerShell?
$example = 'The MAC address is C0-D9-62-39-61-2D'
$mac = [regex]::match($example,'([0-9A-F]{2}[-]){5}([0-9A-F]{2})').value
តើធ្វើដូចម្តេចដើម្បីទាញយកអាសយដ្ឋាន MAC (C0: D9: 62: 39: 61: 2D) ដែលមានសញ្ញាបំបែក “:” ជាមួយ regex ជាមួយ PowerShell?
$example = 'The MAC address is C0:D9:62:39:61:2D'
$mac = [regex]::match($example,'((\d|([a-f]|[A-F])){2}:){5}(\d|([a-f]|[A-F])){2}').value
តើធ្វើដូចម្តេចដើម្បីទាញយកកាលបរិច្ឆេទ (10/02/2015) ជាមួយ regex ជាមួយ PowerShell?
$example = 'The date is 10/02/2015'
$date = [regex]::match($example,'(\d{2}\/\d{2}\/\d{4})').value
តើធ្វើដូចម្តេចដើម្បីទាញយក (www.powershell-guru.com) URL ដោយ regex ជាមួយ PowerShell?
$example = 'The URL is www.powershell-guru.com'
$url = [regex]::match($example,'[a-z]+[:.].*?(?=\s)').value
តើធ្វើដូចម្តេចដើម្បីទាញយកអ៊ីម៉ែល (user@domain.com) ជាមួយ regex ជាមួយ PowerShell?
$example = 'The email is user@domain.com'
$email = [regex]::match($example,'(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b').value
តើធ្វើដូចម្តេចដើម្បីទាញយក “គ្រូ” ខ្សែអក្សរដែលបានពីគំរូដោយមាន PowerShell ជាមួយ regex?
$example = 'www.powershell-guru.com'
[regex]::match($example,'(?<=-)(.*\n?)(?=.com)').value
តើធ្វើដូចម្តេចដើម្បីទាញយក “guru.com” ខ្សែអក្សរដែលបានពីគំរូដោយមាន PowerShell ជាមួយ regex?
$example = 'www.powershell-guru.com'
[regex]::match($example,'(?<=-)(.*\n?)(?<=.)').value
តើធ្វើដូចម្តេចដើម្បីទាញយក “powershell-guru.com” ខ្សែអក្សរដែលបានពីគំរូដោយមាន PowerShell ជាមួយ regex?
$example = 'www.powershell-guru.com'
[regex]::match($example,'(?<=www.)(.*\n?)').value
តើធ្វើដូចម្តេចដើម្បីទាញយក “123” ខ្សែអក្សរដែលបានពីគំរូដោយមាន PowerShell ជាមួយ regex?
$example = 'Powershell123'
[regex]::match($example,'(\d+)').value
តើធ្វើដូចម្តេចដើម្បីទាញយក “$” (សញ្ញាដុល្លារ) ពីឧទាហរណ៍ខ្សែអក្សរនេះជាមួយ regex ជាមួយ PowerShell?
$example = 'Powershell`$123'
[regex]::match($example,'(\$)').value
តើធ្វើដូចម្តេចដើម្បីជំនួសតួអក្សរមួយ (* .com) ជាមួយផ្សេងទៀត (* .fr) នៅក្នុងខ្សែអក្សរដែលបាន regex ជាមួយ PowerShell?
$example = 'www.powershell-guru.com'
[regex]::Replace($example, '.com','.fr')
តើធ្វើដូចម្តេចដើម្បីគេចពីខ្សែអក្សរមួយដោយមាន regex ជាមួយ PowerShell?
[regex]::Escape('\\server\share')
Memory
តើធ្វើដូចម្តេចដើម្បីបង្ខំឱ្យការប្រមូលផ្ដុំនៃការចងចាំដោយការប្រមូលសំរាមដោយមាន PowerShell?
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
តើធ្វើដូចម្តេចដើម្បីទទួលបានទំហំសតិរបស់កុំព្យូទ័រដោយមាន PowerShell?
1 2 3 4 5 6 7 8 |
# Solution 1 Get-CimInstance -ClassName 'cim_physicalmemory' | ForEach-Object -Process {$_.Capacity /1GB} # Solution 2 (Get-WmiObject -Class Win32_ComputerSystem).TotalPhysicalMemory /1GB # Solution 3 (systeminfo.exe | Select-String -Pattern 'Total Physical Memory:').ToString().Split(':')[1].Trim() |
Date
តើធ្វើដូចម្តេចដើម្បីទទួលបានកាលបរិច្ឆេទបច្ចុប្បន្នដោយ PowerShell?
Get-Date
[Datetime]::Now
តើធ្វើដូចម្តេចដើម្បីបង្ហាញកាលបរិច្ឆេទក្នុងទ្រង់ទ្រាយខុសគ្នាជាមួយ 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 |
### DATE ### Get-Date -Format d : 15/04/2015 Get-Date -Format D : 15 April 2015 Get-Date -Format f : 15 April 2015 14:57 Get-Date -Format F : 15 April 2015 14:57:34 Get-Date -Format g : 15/04/2015 14:57 Get-Date -Format G : 15/04/2015 14:57:34 Get-Date -Format yyyyMMdd : 20150415 Get-Date -UFormat '%d%m%Y' : 15042015 Get-Date -UFormat '%m%d%Y' : 04152015 Get-Date -UFormat '%Y%m%d' : 20150415 Get-Date -UFormat '%d%m%Y' : 15-04-2015 Get-Date -UFormat '%m%d%Y' : 04-15-2015 Get-Date -UFormat '%Y%m%d' : 2015-04-15 ### HOUR ### Get-Date -Format t : 15:31 Get-Date -Format T : 15:31:44 Get-Date -Format HH : 15 (Hour) Get-Date -Format mm : 57 (Minute) Get-Date -Format ss : 34 (Seconds) Get-Date -DisplayHint Time : 15:31:44 ### DAY ### Get-Date -Format dddd : Wednesday Get-Date -Format ddd : Wed Get-Date -Format dd : 15 ### MONTH ### Get-Date -Format MMMM : April Get-Date -Format MMM : Apr Get-Date -Format MM : 04 ### YEAR ### Get-Date -Format yyyy : 2015 |
តើធ្វើដូចម្តេចដើម្បីបម្លែងកាលបរិច្ឆេទ (Datetime) មួយដើម្បីកាលបរិច្ឆេទ (ខ្សែអក្សរ) ជាមួយ PowerShell?
$datetimeToString = '{0:dd/MM/yy}' -f (Get-Date 30/01/2015)
$datetimeToString = (Get-Date 31/01/2015).ToShortDateString()
តើធ្វើដូចម្តេចដើម្បីបម្លែងកាលបរិច្ឆេទ (ខ្សែអក្សរ) ទៅជាកាលបរិច្ឆេទ (Datetime) ជាមួយ PowerShell?
$stringToDatetime = [Datetime]::ParseExact('30/01/2015', 'dd/MM/yyyy', $null)
តើធ្វើដូចម្តេចដើម្បីគណនាភាពខុសគ្នា (ចំនួនថ្ងៃម៉ោងនាទីឬវិនាទី) រវាងកាលបរិច្ឆេទពីរជាមួយ PowerShell?
(New-TimeSpan -Start $dateStart -End $dateEnd).Days
(New-TimeSpan -Start $dateStart -End $dateEnd).Hours
(New-TimeSpan -Start $dateStart -End $dateEnd).Minutes
(New-TimeSpan -Start $dateStart -End $dateEnd).Seconds
តើធ្វើដូចម្តេចដើម្បីប្រៀបធៀបកាលបរិច្ឆេទពីរជាមួយ PowerShell?
(Get-Date 2015-01-01) -lt (Get-Date 2015-01-30) # True
(Get-Date 2015-01-01) -gt (Get-Date 2015-01-30) # False
តើធ្វើដូចម្តេចដើម្បីតម្រៀបអារេនៃកាលបរិច្ឆេទជា “Datetime” ជាមួយ PowerShell?
$arrayDate | Sort-Object -Property {$_ -as [Datetime]}
តើធ្វើដូចម្តេចដើម្បីចាប់ផ្តើមជាមួយការបញ្ឈប់ stopwatch PowerShell?
$chrono = [Diagnostics.Stopwatch]::StartNew()
$chrono.Stop()
$chrono
តើធ្វើដូចម្តេចដើម្បីទទួលបាននៅថ្ងៃបច្ចុប្បន្ននៃសប្តាហ៍ជាមួយ PowerShell នេះ?
(Get-Date).DayOfWeek #Sunday
តើធ្វើដូចម្តេចដើម្បីទទួលបានកាលបរិច្ឆេទការកាលពីម្សិលមិញជាមួយ PowerShell?
(Get-Date).AddDays(-1)
តើធ្វើដូចម្តេចដើម្បីទទួលបានចំនួនថ្ងៃក្នុងមួយខែ (នៅក្នុងខែកុម្ភៈឆ្នាំ 2015) ជាមួយ PowerShell?
[DateTime]::DaysInMonth(2015, 2)
តើធ្វើដូចម្តេចដើម្បីឱ្យដឹងថាកាលពីឆ្នាំបង្គ្រប់មួយជាមួយ PowerShell?
[DateTime]::IsLeapYear(2015)
តើធ្វើដូចម្តេចដើម្បីរាយតំបន់ពេលវេលាជាមួយ PowerShell?
[System.TimeZoneInfo]::GetSystemTimeZones()
Networking
តើធ្វើដូចម្តេចដើម្បីសរសេរ (ទៅជាទ្រង់ទ្រាយអក្សរ ASCII) និងបកស្រាយ URL ដោយ PowerShell?
1 2 3 4 5 6 7 8 9 |
# Encode $url = 'http://www.powershell-guru.com' $encoded = [System.Web.HttpUtility]::UrlEncode($url) # Decode $decoded = [System.Web.HttpUtility]::UrlDecode($encoded) # Encoded : http%3a%2f%2fwww.powershell-guru.com # Decoded : http://www.powershell-guru.com |
តើអ្វីទៅសមមូលនៃបណ្តាញពាក្យពញ្ជាដើមជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# ipconfig Get-NetIPConfiguration Get-NetIPAddress # ping Test-NetConnection # tracert Test-NetConnection -TraceRoute # route Get-NetRoute # nslookup Resolve-DnsName # Windows 8.1 & Windows 2012 ([System.Net.Dns]::GetHostEntry($IP)).Hostname # IP > PC ([System.Net.Dns]::GetHostAddresses($computer)).IPAddressToString # PC > IP |
តើធ្វើដូចម្តេចដើម្បីទទួលបានអាសយដ្ឋាន IP ជាមួយ PowerShell?
Get-NetIPAddress # Windows 8.1 & Windows 2012
Get-NetIPConfiguration # Windows 8.1 & Windows 2012
តើធ្វើដូចម្តេចដើម្បីបិទម៉ាស៊ីន V6 អាសយដ្ឋាន IP (IPv6) ជាមួយ PowerShell?
1 |
New-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters' -Name 'DisabledComponents' -Value '0xFFFFFFFF' -PropertyType"DWORD" # Reboot required |
តើធ្វើដូចម្តេចដើម្បីឱ្យមានសុពលភាពជាមួយ V4 អាសយដ្ឋាន IP (IPv4) ជាមួយ PowerShell?
if([ipaddress]'10.0.0.1'){'validated'}
តើធ្វើដូចម្តេចដើម្បីស្វែងរកអាសយដ្ឋាន IP ខាងក្រៅជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Solution 1 (Invoke-WebRequest -Uri 'myexternalip.com/raw').Content (iwr -Uri 'myexternalip.com/raw').Content # Alias # Solution 2 $webClient = New-Object -TypeName System.Net.WebClient $webClient.DownloadString('http://myexternalip.com/raw') # Solution 3 while ($true) { Write-Output -InputObject "$(Get-Date) - $((Invoke-WebRequest -Uri 'http://myexternalip.com/raw' -Method Get).Content)" Start-Sleep -Seconds 300 } |
តើធ្វើដូចម្តេចដើម្បីស្វែងរកឈ្មោះម៉ាស៊ីនពីអាសយដ្ឋាន IP មួយជាមួយ PowerShell នេះ?
([System.Net.Dns]::GetHostEntry($IP)).Hostname
តើធ្វើដូចម្តេចដើម្បីស្វែងរកអាសយដ្ឋាន IP ដែលបានមកពីឈ្មោះម៉ាស៊ីនដោយប្រើ PowerShell?
([System.Net.Dns]::GetHostAddresses($computer)).IPAddressToString
របៀបស្វែងរកបានពីឈ្មោះម៉ាស៊ីន FQDN ដោយមាន PowerShell?
[System.Net.Dns]::GetHostByName($computer).HostName
1 |
Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Format-Table -Property Description, IpAddress, IPSubnet, DefaultIPGateway, DNSServerSearchOrder |
តើធ្វើដូចម្តេចដើម្បីស្វែងរកអាសយដ្ឋាន MAC ជាមួយ PowerShell?
Get-CimInstance win32_networkadapterconfiguration | Select-Object -Property Description, Macaddress
Get-WmiObject -Class win32_networkadapterconfiguration | Select-Object -Property Description, Macaddress
តើធ្វើដូចម្តេចដើម្បី កុំព្យូទ័រឮសូរតឺងជាមួយ PowerShell?
1 2 3 4 5 6 |
# Solution 1 Test-Connection -ComputerName $computer -Quiet # Returns True / False # Solution 2 $ping = New-Object -TypeName System.Net.Networkinformation.Ping $ping.Send($computer) |
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលប្រសិនបើកុំព្យូទ័រមួយដែលត្រូវបានតភ្ជាប់ទៅអ៊ីនធឺណិតជាមួយ PowerShell?
1 |
[Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]'{DCB00C01-570F-4A9B-8D69-199FDBA5723B}')).IsConnectedToInternet |
តើធ្វើដូចម្តេចដើម្បីធ្វើការរកមើល”whois” សម្រាប់វែបសាយជាមួយ PowerShell?
$whois = New-WebServiceProxy 'http://www.webservicex.net/whois.asmx?WSDL'
$whois.GetWhoIs('powershell-guru.com')
តើធ្វើដូចម្តេចដើម្បីទទួលបានពត៌មានលំអិតនៃ IP របស់សាធារណៈ (ទីតាំងភូមិសាស្ដ្រ) ជាមួយ PowerShell?
1 2 |
$externalIP = (Invoke-WebRequest -Uri 'myexternalip.com/raw').Content $detailsIP = ([xml](Invoke-WebRequest -Uri "http://freegeoip.net/xml/$externalIP" -UseBasicParsing).Content).Response |
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលប្រសិនបើកំពង់ផែមួយគឺបើក/បិទជាមួយ PowerShell?
New-Object -TypeName Net.Sockets.TcpClient -ArgumentList $computer, 135
តើធ្វើដូចម្តេចដើម្បីអនុវត្ត “tracert” ជាមួយ PowerShell?
Test-NetConnection www.google.com -TraceRoute
តើធ្វើដូចម្តេចដើម្បីជួសជុលទម្រង់តភ្ជាប់បណ្តាញផ្ទះជាមួយ PowerShell?
Get-NetAdapter | Format-Table -Property Name, InterfaceDescription, ifIndex -AutoSize # Windows 8.1
Set-NetConnectionProfile -InterfaceIndex 6 -NetworkCategory Private
តើធ្វើដូចម្តេចដើម្បីបង្ហាញពីការតភ្ជាប់ច្រក TCP ជាមួយ PowerShell?
netstat.exe -ano
Get-NetTCPConnection #Windows 8 and 2012
តើធ្វើដូចម្តេចដើម្បីឱ្យ URL ខ្លីចូលទៅក្នុង URL វែងជាមួយ PowerShell?
$url = 'www.powershell-guru.com'
$tiny = Invoke-RestMethod -Uri "http://tinyurl.com/api-create.php?url=$url"
តើធ្វើដូចម្តេចដើម្បីទទួលបានការកំណត់ប្រូកស៊ីដោយមាន PowerShell?
Get-ItemProperty -Path HKCU:"Software\Microsoft\Windows\CurrentVersion\Internet Settings"
DNS
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលឃ្លាំងសម្ងាត់ DNS ដែលនៅលើកុំព្យូទ័រមូលដ្ឋានដោយមាន PowerShell?
ipconfig.exe /displaydns
Get-DnsClientCache #Windows 8 and 2012
តើធ្វើដូចម្តេចដើម្បីជម្រះឃ្លាំងសម្ងាត់ DNS មួយនៅលើកុំព្យូទ័រមូលដ្ឋានដោយមាន PowerShell?
ipconfig.exe /flushdns
Start-Process -FilePath ipconfig -ArgumentList /flushdns -WindowStyle Hidden
Clear-DnsClientCache #Windows 8 and 2012
តើធ្វើដូចម្តេចដើម្បីជម្រះឃ្លាំងសម្ងាត់ DNS មួយនៅលើកុំព្យូទ័រពីចម្ងាយជាមួយ PowerShell?
Invoke-Command -ScriptBlock {Clear-DnsClientCache} -ComputerName computer01, computer02
តើធ្វើដូចម្តេចដើម្បីអានឯកសារជាមួយម៉ាស៊ីន PowerShell?
Get-Content -Path 'C:\Windows\system32\drivers\etc\hosts'
Password
តើធ្វើដូចម្តេចដើម្បីបង្កើតពាក្យសម្ងាត់ចៃដន្យជាមួយការ PowerShell?
[Reflection.Assembly]::LoadWithPartialName('System.Web')
[System.Web.Security.Membership]::GeneratePassword(30,2)
តើធ្វើដូចម្តេចដើម្បីផ្លាស់ប្តូរពាក្យសម្ងាត់អ្នកគ្រប់គ្រងក្នុងស្រុកនៅលើម៉ាស៊ីនបម្រើពីចម្ងាយជាមួយ PowerShell?
$admin = [ADSI]('WinNT://server01/administrator,user')
$admin.SetPassword($password)
$admin.SetInfo()
តើធ្វើដូចម្តេចដើម្បីស្វែងរកកាលបរិច្ឆេទផុតកំណត់ពាក្យសម្ងាត់នៃគណនីក្នុងថតសកម្មដោយមាន PowerShell?
1 2 3 4 5 6 7 8 |
# Solution 1 [DateTime]::FromFileTime((Get-ADUser -Identity $user -Properties 'msDS-UserPasswordExpiryTimeComputed').'msDS-UserPasswordExpiryTimeComputed') # Solution 2 Get-Date -Date ((Get-ADUser $user -Properties 'msDS-UserPasswordExpiryTimeComputed' | Select-Object -Property @{ Name = 'ExpiryDate' Expression = {[DateTime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')} }).ExpiryDate)-Format 'F' |
Printers
តើធ្វើដូចម្តេចដើម្បីរាយម៉ាស៊ីនបោះពុម្ពទាំងអស់សម្រាប់ម៉ាស៊ីនបម្រើជាក់លាក់មួយជាមួយ PowerShell?
Get-WmiObject -Query 'Select * From Win32_Printer' -ComputerName $computer
តើធ្វើដូចម្តេចដើម្បីរាយគ្រប់ទាំងច្រកសម្រាប់ម៉ាស៊ីនបម្រើជាក់លាក់មួយជាមួយ PowerShell?
Get-WmiObject -Class Win32_TCPIPPrinterPort -Namespace 'root\CIMV2' -ComputerName $computer
តើធ្វើដូចម្តេចដើម្បីផ្លាស់ប្តូរមតិយោបល់/ទីតាំងរបស់ម៉ាស៊ីនបោះពុម្ពជាមួយ PowerShell?
1 2 3 4 |
$printer = Get-WmiObject -Class win32_printer -Filter "Name='HP Deskjet 2540 series'" $printer.Location = 'Germany' $printer.Comment = 'Printer - Test' $printer.Put() |
តើធ្វើដូចម្តេចដើម្បីលុប (ការលុបចោលការងារទាំងអស់) ទៅម៉ាស៊ីនបោះពុម្ពដោយមាន PowerShell?
$printer = Get-WmiObject -Class win32_printer -Filter "Name='HP Deskjet 2540 series'"
$printer.CancelAllJobs()
តើធ្វើដូចម្តេចដើម្បីបោះពុម្ពសម្រាប់ម៉ាស៊ីនបោះពុម្ពដោយមានទំព័រសាកល្បងជាមួយ PowerShell?
$printer = Get-WmiObject -Class win32_printer -Filter "Name='HP Deskjet 2540 series'"
$printer.PrintTestPage()
តើធ្វើដូចម្តេចដើម្បីទទួលបានជួរបោះពុម្ពសម្រាប់ម៉ាស៊ីនបោះពុម្ពជាមួយ PowerShell?
1 2 3 4 |
Get-WmiObject -Class Win32_PerfFormattedData_Spooler_PrintQueue | Select-Object -Property Name, @{ Expression = {$_.jobs} Label = 'Current Jobs' } | Format-Table -AutoSize |
Regedit
Read
តើធ្វើដូចម្តេចដើម្បីរាយបញ្ជីឈ្មោះផ្នូចជាមួយ PowerShell?
Get-ChildItem -Path Registry::
តើធ្វើដូចម្តេចដើម្បីទទួលបានតម្លៃចុះបញ្ជីនិងប្រភេទតម្លៃជាមួយ 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 |
function Get-RegistryValue { Param ( [Parameter(Mandatory = $true)] [string]$RegistryKey ) $key = Get-Item -Path "Registry::$RegistryKey" $key.GetValueNames() | Sort-Object | ForEach-Object -Process { $name = $_ $type = $key.GetValueKind($name) switch ($type) { 'String' {'REG_SZ'} 'Binary' {'REG_BINARY'} 'Dword' {'REG_DWORD'} 'Qword' {'REG_QWORD'} 'MultiString' {'REG_MULTI_SZ'} 'ExpandString'{'REG_EXPAND_SZ'} Default {$null} } [PSCustomObject]@{ Name = $name Type = $type Data = $key.GetValue($name) } } } |
តើធ្វើដូចម្តេចដើម្បីរាយ subkeys គន្លឹះបញ្ជីឈ្មោះជាមួយ PowerShell?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#HKEY_CLASSES_ROOT New-PSDrive -PSProvider Registry -Root HKEY_CLASSES_ROOT -Name HKCR Get-ChildItem -Path 'HKCR:\' #HKEY_CURRENT_USER Get-ChildItem -Path 'HKCU:\Software' Get-ChildItem -Path Registry::HKEY_CURRENT_USER #HKEY_LOCAL_MACHINE Get-ChildItem -Path 'HKLM:\SYSTEM' Get-ChildItem -Path Registry::HKEY_LOCAL_MACHINE #HKEY_USERS New-PSDrive -PSProvider Registry -Root HKEY_USERS -Name HKU Get-ChildItem -Path 'HKU:\' #HKEY_CURRENT_CONFIG New-PSDrive -PSProvider Registry -Root HKEY_CURRENT_CONFIG -Name HKCC Get-ChildItem -Path 'HKCC:\' |
តើធ្វើដូចម្តេចដើម្បីរាយ subkeys គន្លឹះបញ្ជីឈ្មោះនៅក្នុងវិធីដែលហៅខ្លួនឯងជាមួយ PowerShell?
Get-ChildItem -Path 'HKLM:\SYSTEM' -Recurse -ErrorAction SilentlyContinue
តើធ្វើដូចម្តេចដើម្បីស្វែងរក subkeys ដែលមានឈ្មោះជាក់លាក់មួយដោយ PowerShell?
Get-ChildItem -Path 'HKLM:\SOFTWARE' -Include *Plugin* -Recurse -ErrorAction SilentlyContinue
តើធ្វើដូចម្តេចដើម្បីត្រឡប់តែឈ្មោះរបស់ subkeys បញ្ជីឈ្មោះនេះជាមួយ PowerShell ?
(Get-ChildItem -Path 'HKLM:\SYSTEM').Name # Return HKEY_LOCAL_MACHINE\SYSTEM\ControlSet
Get-ChildItem -Path 'HKLM:\SYSTEM' -Name # Return ControlSet
តើធ្វើដូចម្តេចដើម្បីរាយតម្លៃចុះបញ្ជីជាមួយ PowerShell?
Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
តើធ្វើដូចម្តេចដើម្បីអានបញ្ជីឈ្មោះមួយដែលមានតម្លៃជាក់លាក់ជាមួយ PowerShell?
(Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion').ProductName
តើធ្វើដូចម្តេចដើម្បីអានគុណតម្លៃបញ្ជីឈ្មោះជាក់លាក់នៅលើកុំព្យូទ័រពីចម្ងាយជាមួយ PowerShell?
1 2 3 4 5 |
$hostname = $computer $openRegedit = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $hostname) $openKey = $openRegedit.OpenSubKey('SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion') $keyValue = $openKey.GetValue('ProductName') $keyValue |
Write
តើធ្វើដូចម្តេចដើម្បីបង្កើតគន្លឹះបញ្ជីឈ្មោះថ្មីមួយជាមួយ PowerShell?
New-Item -Path 'HKCU:\Software\MyApplication'
តើធ្វើដូចម្តេចដើម្បីបង្កើតតម្លៃបញ្ជីឈ្មោះមួយដែលមាន PowerShell?
New-ItemProperty -Path 'HKCU:\Software\MyApplication' -Name 'Version' -Value '1.0'
តើធ្វើដូចម្តេចដើម្បីកែប្រែគុណតម្លៃដែលមានស្រាប់ជាមួយនឹងបញ្ជីឈ្មោះ PowerShell?
Set-ItemProperty -Path 'HKCU:\Software\MyApplication' -Name 'Version' -Value '2.0'
Delete
តើធ្វើដូចម្តេចដើម្បីលុបតម្លៃបញ្ជីឈ្មោះមួយដែលមាន PowerShell?
Remove-ItemProperty -Path 'HKCU:\Software\MyApplication' -Name 'Version'
តើធ្វើដូចម្តេចដើម្បីលុបសោចុះបញ្ជីជាមួយ PowerShell?
Remove-Item -Path 'HKCU:\Software\MyApplication' -Force
Test
តើធ្វើដូចម្តេចដើម្បីសាកល្បងបើគន្លឹះបញ្ជីឈ្មោះដែលមានជាមួយ PowerShell?
Test-Path -Path 'HKCU:\Software\MyApplication'
តើធ្វើដូចម្តេចដើម្បីសាកល្បងបើតម្លៃដែលមានជាមួយនឹងបញ្ជីឈ្មោះមួយដែល PowerShell?
(Get-Item -Path 'HKCU:\Software\MyApplication').GetValueNames() -contains 'Version'
Strings
តើធ្វើដូចម្តេចដើម្បីយកតួអក្សរពណ៌សជាចន្លោះពីការចាប់ផ្តើមនៃខ្សែអក្សរជាមួយ PowerShell?
$string = ' PowershellGuru'
$string = $string.TrimStart()
តើធ្វើដូចម្តេចដើម្បីយកតួអក្សរពណ៌សជាចន្លោះពីចុងបញ្ចប់នៃខ្សែអក្សរជាមួយ PowerShell?
$string = 'PowershellGuru '
$string = $string.TrimEnd()
តើធ្វើដូចម្តេចដើម្បីយកតួអក្សរពណ៌សចន្លោះ (ការចាប់ផ្តើមនិងបញ្ចប់) នៃខ្សែអក្សរជាមួយ PowerShell?
$string = ' PowershellGuru '
$string = $string.Trim()
តើធ្វើដូចម្តេចដើម្បីបម្លែងទៅជាខ្សែអក្សរមួយទៅនឹងករណីខាងលើជាមួយ PowerShell?
$string = 'powershellguru'
$string = $string.ToUpper()
តើធ្វើដូចម្តេចដើម្បីបម្លែងទៅជាខ្សែអក្សរដើម្បីបន្ថយករណីជាមួយ PowerShell?
$string = 'POWERSHELLGURU'
$string = $string.ToLower()
តើធ្វើដូចម្តេចដើម្បីជ្រើសខ្សែអក្សររង “PowerShell” នៃខ្សែអក្សរ “PowerShellGuru” ជាមួយ PowerShell?
$string.Substring(0,10)
តើធ្វើដូចម្តេចដើម្បីជ្រើសខ្សែអក្សររងដែលបាន “លោក Guru” នៃខ្សែអក្សរ “PowerShellGuru” ជាមួយ PowerShell?
$string.Substring(10)
តើធ្វើដូចម្តេចដើម្បីជ្រើស “123” ចំនួននៃ “PowerShell123Guru” ជាមួយ PowerShell?
$string = 'Powershell123Guru'
[regex]::match($string,'(\d+)').value
តើធ្វើដូចម្តេចដើម្បីទទួលបានលិបិក្រមនៅមូលដ្ឋានសូន្យនៃ “Guru” នៃខ្សែអក្សរ “PowerShellGuru” ជាមួយ PowerShell?
$string.IndexOf('Guru') # 10
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលប្រសិនបើខ្សែអក្សរគឺទទេឬទទេជាមួយ PowerShell?
$string = $null
$string = ''
[string]::IsNullOrEmpty($string)
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលប្រសិនបើខ្សែអក្សរមួយគឺទទេ, ទទេ, ឬមានតែតួអក្សរដែលមានពណ៌សជាចន្លោះជាមួយ PowerShell?
$string = $null
$string = ''
$string = ' '
[string]::IsNullOrWhiteSpace($string)
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលប្រសិនបើខ្សែអក្សរមានលិខិតជាក់លាក់មួយជាមួយ PowerShell?
$string = 'PowershellGuru'
$string.Contains('s')
[regex]::match($string,'s').Success
តើធ្វើដូចម្តេចដើម្បីត្រឡប់ប្រវែងនៃខ្សែអក្សរជាមួយ PowerShell?
$string.Length
តើធ្វើដូចម្តេចដើម្បី concatenate ខ្សែអក្សរពីរជាមួយ PowerShell?
1 2 3 4 5 6 7 |
# Solution 1 $string1 + $string2 # Solution 2 $string1 = 'Powershell' $string2 = 'Guru' [string]::Concat($string1,$string2) |
តើធ្វើដូចម្តេចដើម្បីផ្គូផ្គងមួយឬតង្កៀបជាច្រើន “[]” នៅក្នុងខ្សែអក្សរជាមួយ PowerShell?
$string = '[PowershellGuru]'
$string -match '\[' # Only 1
$string -match '\[(.*)\]' # Several
តើធ្វើដូចម្តេចដើម្បីផ្គូផ្គងមួយឬច្រើនដែលក្រចក» () “នៅក្នុងខ្សែអក្សរជាមួយ PowerShell?
$string = '(PowershellGuru)'
$string -match '\(' # Only 1
$string -match '\((.*)\)' # Several
តើធ្វើដូចម្តេចដើម្បីផ្គូផ្គងដង្កៀតអង្កាញ់មួយឬ ច្រើនបាន “{}” នៅក្នុងខ្សែអក្សរជាមួយ PowerShell?
$string = '{PowershellGuru}'
$string -match '\{' # Only 1
$string -match '\{(.*)\}' # Several
តើធ្វើដូចម្តេចដើម្បីផ្គូផ្គងសម្រាប់តង្កៀបមុំមួយឬច្រើនដែល “<>” នៅក្នុងខ្សែអក្សរជាមួយ PowerShell?
$string = ''
$string -match '\<' # Only 1
$string -match "\<(.*)\>" # Several
តើធ្វើដូចម្តេចដើម្បីផ្គូផ្គងតួអក្សរតូចណាមួយ (ABC) នៅក្នុងខ្សែអក្សរជាមួយ PowerShell?
$string = 'POWERSHELLGURU'
$string -cmatch "^[a-z]*$" #False
តើធ្វើដូចម្តេចដើម្បីផ្គូផ្គង upperletters ណាមួយ (ABC) នៅក្នុងខ្សែអក្សរជាមួយ PowerShell?
$string = 'powershellguru'
$string -cmatch "^[A-Z]*$" #False
តើធ្វើដូចម្តេចដើម្បីផ្គូផ្គង “ [P “(p ករណីទាបជាង) នៅក្នុងខ្សែអក្សរជាមួយ PowerShell?
$string = '[powershellGuru]'
$string -cmatch '\[[a-z]\w+' #True
តើធ្វើដូចម្តេចដើម្បីផ្គូផ្គង “ [P បាន “(P ករណីខ្ពស់) នៅក្នុងខ្សែអក្សរជាមួយ PowerShell?
$string = '[PowershellGuru]'
$string -cmatch '\[[A-Z]\w+' #True
តើធ្វើដូចម្តេចដើម្បីជំនួសបន្ទាត់ដែលមានបន្ទាត់មួយផ្សេងទៀតជាមួយ PowerShell?
$a = 'Line A'
$b = 'Line B'
$a = $a -replace $a, $b
តើធ្វើដូចម្តេចដើម្បីបម្លែងទៅជាការប្រតិបត្ដិការបែងចែកទៅជាខ្សែអក្សរ (ភាគរយ) ជាមួយ PowerShell?
(1/2).ToString('P')
តើធ្វើដូចម្តេចដើម្បីតម្រៀបខ្សែអក្សរដែលមានលេខជាមួយ PowerShell?
1 |
'string-10', 'string-2', 'string-23', 'string-30' | Sort-Object -Property {$_ -replace '[\d]'}, {$_ -replace '[a-zA-Z\p{P}]'-as [int]} |
តើធ្វើដូចម្តេចដើម្បីជ្រើសពាក្យចុងក្រោយនៃប្រយោគជាមួយ PowerShell?
$sentence = 'My name is Test Powershell'
$sentence.Split(' ')[-1] # Returns Powershell
តើធ្វើដូចម្តេចដើម្បីទទួលបានពាក្យដ៏ធំបំផុតនៃប្រយោគជាមួយ PowerShell?
$sentence = 'My name is Test Powershell'
$sentence.Split(' ') | Sort-Object -Property Length | Select-Object -Last 1 # Returns Powershell
តើធ្វើដូចម្តេចដើម្បីរាប់ចំនួនដងនៃខ្សែអក្សរមួយដែលត្រូវបង្ហាញក្នុងប្រយោគជាមួយ PowerShell?
$sentence = 'test test test Powershell'
[regex]::Matches($sentence, 'test').Count # Returns 3
តើធ្វើដូចម្តេចដើម្បីចម្លងតួអក្សរនីមួយៗនៅក្នុងខ្សែអក្សរមួយទៅលំដាប់តួអក្សរមួយជាមួយ PowerShell?
1 2 3 4 5 6 7 |
$name = 'test' $name.ToCharArray() s t e v e |
តើធ្វើដូចម្តេចដើម្បីបម្លែងទៅជាអក្សរធំដំបូងទៅជាអក្សរធំនៃខ្សែអក្សរជាមួយ PowerShell?
1 2 |
$name = 'test' $name.Substring(0,1).ToUpper() + $name.Substring(1) |
តើធ្វើដូចម្តេចដើម្បីដំណើការខ្សែអក្សរ (ឆ្វេងឬស្ដាំ) ជាមួយ PowerShell?
1 2 3 4 5 6 7 |
# With whitespaces $padRight = 'test'.PadRight(25) $padLeft = 'test'.PadLeft(25) # With characters $padRight = 'test'.PadRight(25,'.') # Return test.................... $padLeft = 'test'.PadLeft(25,'.') # Return ....................test |
តើធ្វើដូចម្តេចដើម្បីសរសេរ និងបកស្រាយខ្សែអក្សរមួយ Base64 ជាមួយ PowerShell ?
1 2 3 4 5 6 7 8 9 10 |
# Encode $string = [System.Text.Encoding]::UTF8.GetBytes('test') $encoded = [System.Convert]::ToBase64String($string) # Decode $string = [System.Convert]::FromBase64String($encoded) $decoded = [System.Text.Encoding]::UTF8.GetString($string) # Encoded : c3RldmU= # Decoded : test |
តើធ្វើដូចម្តេចដើម្បីបម្លែងទៅជាចំនួនលេខ (ទៅ និងមកពី) គោលពីរជាមួយ PowerShell?
1 2 3 4 5 |
# Base 10 to Base 2 [System.Convert]::ToString(255,2) # Base 2 to Base 10 [System.Convert]::ToInt32('11111111',2) |
តើធ្វើដូចម្តេចដើម្បីយកតែថតមេចុងក្រោយពីក្នុងផ្លូវមកវិញជាមួយ PowerShell?
1 2 |
$path = 'C:\Folder1\Folder2\Folder3\file.txt' Split-Path -Path (Split-Path -Path $path -Parent) -Leaf # Return Folder3 |
តើធ្វើដូចម្តេចដើម្បីយកតែធាតុចុងក្រោយពីក្នុងផ្លូវមកវិញជាមួយ PowerShell?
1 2 |
$path = 'C:\Folder1\Folder2\Folder3\file.txt' Split-Path -Path $path -Leaf # Return file.txt |
Math
តើធ្វើដូចម្តេចដើម្បីរាយវិធីសាស្រ្តនៃថ្នាក់ System.Math នេះជាមួយ PowerShell?
[System.Math] | Get-Member -Static -MemberType Method
តើធ្វើដូចម្តេចដើម្បីត្រឡប់តម្លៃដាច់ខាតជាមួយ PowerShell?
[Math]::Abs(-12) #Returns 12
[Math]::Abs(-12.5) # Returns 12.5
តើធ្វើដូចម្តេចដើម្បីត្រឡប់មុំដែលមានស៊ីនុសគឺលេខដែលបានបញ្ជាក់ជាមួយ PowerShell ?
[Math]::ASin(1) #Returns 1,5707963267949
តើធ្វើដូចម្តេចដើម្បីត្រឡប់តម្លៃពិដានជាមួយ PowerShell?
[Math]::Ceiling(1.4) #Returns 2
[Math]::Ceiling(1.9) #Returns 2
តើធ្វើដូចម្តេចដើម្បីត្រឡប់តម្លៃជាន់ជាមួយ PowerShell?
[Math]::Floor(1.4) #Returns 1
[Math]::Floor(1.9) #Returns 1
តើធ្វើដូចម្តេចដើម្បីត្រឡប់ (គោល e) លោការីតធម្មជាតិនៃលេខដែលបានបញ្ជាក់ជាមួយ PowerShell ?
[Math]::Log(4) #Returns 1,38629436111989
តើធ្វើដូចម្តេចដើម្បីត្រឡប់លោការីតគោល 10 នៃចំនួនដែលបានបញ្ជាក់ជាមួយ PowerShell?
[Math]::Log10(4) #Returns 0,602059991327962
តើធ្វើដូចម្តេចដើម្បីត្រឡប់អតិបរមានៃតម្លៃពីរជាមួយ PowerShell?
[Math]::Max(2,4) #Returns 4
[Math]::Max(-2,-4) #Returns -2
តើធ្វើដូចម្តេចដើម្បីត្រឡប់អប្បរមានៃតម្លៃពីរជាមួយ PowerShell?
[Math]::Min(2,4) #Returns 2
[Math]::Max(-2,-4) #Returns -4
តើធ្វើដូចម្តេចដើម្បីត្រឡប់អំណាចដែលបានបង្កើតជាក់លាក់ជាមួយ PowerShell?
[Math]::Pow(2,4) #Returns 16
តើធ្វើដូចម្តេចដើម្បីត្រឡប់ជាតម្លៃទសភាគទៅតម្លៃដែលសំខាន់ដែលនៅជិតបំផុតជាមួយ PowerShell?
[Math]::Round(3.111,2) #Returns 3,11
[Math]::Round(3.999,2) #Returns 4
តើធ្វើដូចម្តេចដើម្បីត្រឡប់ផ្នែកដ៏សំខាន់នៃចំនួនទសភាគដែលបានបញ្ជាក់ជាមួយ PowerShell?
[Math]::Truncate(3.111) #Returns 3
[Math]::Truncate(3.999) #Returns 3
តើធ្វើដូចម្តេចដើម្បីត្រឡប់ឫសការ៉េនៃលេខដែលបានបញ្ជាក់មួយជាមួយ PowerShell?
[Math]::Sqrt(16) #Returns 4
តើធ្វើដូចម្តេចដើម្បីវិលត្រឡប់ថេរភីអាយមកវិញជាមួយ PowerShell?
[Math]::Pi #Returns 3,14159265358979
តើធ្វើដូចម្តេចដើម្បីត្រឡប់លោការីតធម្មជាតិមូលដ្ឋាន (អ៊ីថេរ) ជាមួយ PowerShell?
[Math]::E #Returns 2,71828182845905
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលបើលេខមួយគឺសូម្បីតែគត់ឬសេសជាមួយ PowerShell ?
[bool]($number%2)
Hashtables
តើធ្វើដូចម្តេចដើម្បីបង្កើត hashtable ទទេជាមួយ PowerShell?
$hashtable = @{}
$hashtable = New-Object -TypeName System.Collections.Hashtable
តើធ្វើដូចម្តេចដើម្បីបង្កើត hashtable ជាមួយធាតុមួយជាមួយ PowerShell?
1 2 3 4 5 |
$hashtable = @{ 'Key1' = 'Value1' 'Key2' = 'Value2' 'Key3' = 'Value3' } |
1 2 3 4 5 6 7 |
$hashtable = [ordered]@{ 'Key1' = 'Value1' 'Key2' = 'Value2' 'Key3' = 'Value3' } $hashtable | Get-Member # System.Collections.Specialized.OrderedDictionary |
តើធ្វើដូចម្តេចដើម្បីបន្ថែមធាតុ (គូតម្លៃគ្រាប់ចុច) ដើម្បី hashtable ជាមួយ PowerShell?
$hashtable.Add('Key3', 'Value3')
តើធ្វើដូចម្តេចដើម្បីទទួលបាននូវតម្លៃជាក់លាក់នៃ hashtable ជាមួយ PowerShell?
$hashtable.Key1
$hashtable.Get_Item('Key1')
តើធ្វើដូចម្តេចដើម្បីទទួលបានតម្លៃអប្បបរមារបស់ hashtable ជាមួយ PowerShell?
1 2 3 4 5 6 7 8 |
$hashtable = @{ 'Key1' = '1' 'Key2' = '2' 'Key3' = '3' } $hashtable.GetEnumerator() | Sort-Object -Property Value | Select-Object -First 1 $hashtable.GetEnumerator() | Sort-Object -Property Value -Descending | Select-Object -Last 1 |
តើធ្វើដូចម្តេចដើម្បីទទួលបានតម្លៃអតិបរមារបស់ hashtable ជាមួយ PowerShell?
1 2 3 4 5 6 7 8 |
$hashtable = @{ 'Key1' = '1' 'Key2' = '2' 'Key3' = '3' } $hashtable.GetEnumerator() | Sort-Object -Property Value -Descending | Select-Object -First 1 $hashtable.GetEnumerator() | Sort-Object -Property Value | Select-Object -Last 1 |
តើធ្វើដូចម្តេចដើម្បីកែប្រែធាតុនៅក្នុង hashtable ជាមួយ PowerShell?
$hashtable.Set_Item('Key1', 'Value1Updated')
តើធ្វើដូចម្តេចដើម្បីយកចេញធាតុនៅក្នុង hashtable ជាមួយ PowerShell?
$hashtable.Remove('Key1')
តើធ្វើដូចម្តេចដើម្បីជម្រះ hashtable ជាមួយ PowerShell?
$hashtable.Clear()
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលវត្តមានរបស់ដ៏សំខាន់មួយជាក់លាក់/តម្លៃក្នុង hashtable ជាមួយ PowerShell?
$hashtable.ContainsKey('Key3')
$hashtable.ContainsValue('Value3')
តើធ្វើដូចម្តេចដើម្បីតម្រៀបដោយគន្លឹះ/តម្លៃក្នុង hashtable ជាមួយ PowerShell?
$hashtable.GetEnumerator() | Sort-Object -Property Name
$hashtable.GetEnumerator() | Sort-Object -Property Value -Descending
Arrays
តើធ្វើដូចម្តេចដើម្បីបង្កើតជាអារេទទេជាមួយ PowerShell?
$array = @()
$array = [System.Collections.ArrayList]@()
តើធ្វើដូចម្តេចដើម្បីបង្កើតអារេដែលមានធាតុមួយជាមួយ PowerShell?
$array = @('A', 'B', 'C')
$array = 'A', 'B', 'C'
$array = 'a,b,c'.Split(',')
$array = .{$args} a b c
$array = echo a b c
តើធ្វើដូចម្តេចដើម្បីបន្ថែមធាតុទៅអារេមួយជាមួយ PowerShell?
$array += 'D'
[void]$array.Add('D')
តើធ្វើដូចម្តេចដើម្បីកែប្រែធាតុមួយក្នុងអារេមួយជាមួយ PowerShell?
$array[0] = 'Z' # 1st item[0]
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលទំហំនៃអារេមួយជាមួយ PowerShell?
$array = 'A', 'B', 'C'
$array.Length # Returns 3
តើធ្វើដូចម្តេចដើម្បីទាញយកធាតុមួយ/ច្រើន/ទាំងអស់នៅក្នុងអារេមួយជាមួយ PowerShell?
$array = @('A', 'B', 'C')
$array[0] # One item (A)
$array[0] + $array[2] # Several items (A,C)
$array # All items (A,B,C)
តើធ្វើដូចម្តេចដើម្បីយកធាតុទទេមួយក្នុងអារេជាមួយ PowerShell?
$array = @('A', 'B', 'C', '')
$array = $array.Split('',[System.StringSplitOptions]::RemoveEmptyEntries) | Sort-Object # A,B,C
តើធ្វើដូចម្តេចដើម្បីពិនិត្យមើលប្រសិនបើធាតុមួយមាននៅក្នុងអារេមួយជាមួយ PowerShell?
$array = @('A', 'B', 'C')
'A' | ForEach-Object -Process {$array.Contains($_)} # Returns True
'D' | ForEach-Object -Process {$array.Contains($_)} # Returns False
តើធ្វើដូចម្តេចដើម្បីរកចំនួនលិបិក្រមនៃធាតុមួយក្នុងអារេមួយជាមួយ PowerShell?
$array = @('A', 'B', 'C')
[array]::IndexOf($array,'A') # Returns 0
តើធ្វើដូចម្តេចដើម្បីផ្លាស់ប្តូរលំដាប់នៃធាតុនៅក្នុងអារេមួយជាមួយ PowerShell?
$array = @('A', 'B', 'C')
[array]::Reverse($array) # C,B,A
តើធ្វើដូចម្តេចដើម្បីបង្កើតធាតុចៃដន្យពីអារេមួយជាមួយ PowerShell?
$array | Get-Random
តើធ្វើដូចម្តេចដើម្បីតម្រៀបអារេមួយនៅក្នុងការតម្រៀបឡើង/ចុះផ្លូវជាមួយ PowerShell?
$array = @('A', 'B', 'C')
$array | Sort-Object # A,B,C
$array | Sort-Object -Descending # C,B,A
តើធ្វើដូចម្តេចដើម្បីរាប់ចំនួនធាតុនៅក្នុងអារេមួយជាមួយ PowerShell?
$array.Count
តើធ្វើដូចម្តេចដើម្បីបន្ថែមអារេមួយទៅមួយផ្សេងទៀតជាមួយ PowerShell?
$array1 = 'A', 'B', 'C'
$array2 = 'D', 'E', 'F'
$array3 = $array1 + $array2 # A,B,C,D,E,F
តើធ្វើដូចម្តេចដើម្បីស្វែងរកភាពស្ទួនគ្នាពីអារេមួយជាមួយ PowerShell?
$array = 'A', 'B', 'C', 'C'
($array | Group-Object | Where-Object -FilterScript {$_.Count -gt 1}).Values # Returns C
តើធ្វើដូចម្តេចដើម្បីយកចេញស្ទួនពីអារេមួយជាមួយ PowerShell?
$array = 'A', 'B', 'C', 'C'
$array = $array | Select-Object -Unique
$array # Returns A,B,C
តើធ្វើដូចម្តេចដើម្បីបង្កើតអារេមួយដែលបានចាប់ផ្តើមដោយមានធាតុជាបុព្វបទមួយ (“user01”, “user02” … “user10”) ជាមួយ PowerShell?
$array = 1..10 | ForEach-Object -Process { "user$_" }
ACL
តើធ្វើដូចម្តេចដើម្បីរាយ ACL នៃ AD អ្នកប្រើជាមួយ PowerShell ?
(Get-Acl -Path "AD:\$dn").Access
តើធ្វើដូចម្តេចដើម្បីរាយ ACL នៃថតជាមួយ PowerShell?
(Get-Acl -Path C:\scripts).Access