顯示具有 IT PRGO PowerShell 標籤的文章。 顯示所有文章
顯示具有 IT PRGO PowerShell 標籤的文章。 顯示所有文章

3/23/2022

Powershell Print 的參數說明

 

參數
說明
`o
字串結束符號,此符號無法列印。
`a
警告符號,ASCII碼為7的字元。
`b
退格,刪除前一個字元。
`f
換頁或分頁符號,會讓印表機在新的一頁上開始列印。
`n
分行符號,使印表機或終端在新的一行開始列印。
`r
Return符號
`t
水平定位字元,同功能。
`v
垂直定位字元

如何利用 Powershell 設定檔案屬性

 Powershell 提供兩種方式設定檔案屬性的設定值。

  • 屬性名稱表示
  • $file = Get-Item .\x.exe
    $file.attributes = "ReadOnly"

  • 屬性數值表示(16位元數值)
  • $file = Get-Item .\x.exe
    $file.attributes = 0x20

兩者的對照表如下:
屬性名稱
屬性數值
ReadOnly
0x01
Hidden
0x02
System
0x04
Directory
0x10
Archive
0x20
Device
0x40
Normal
0x80
Temporary
0x100
SparseFile
0x200
ReparsePoint
0x400
Compressed
0x800

依照以上的說明,我們也可以同時設定兩組以上屬性。
  • 屬性名稱表示
  • $file = Get-Item .\x.exe
    $file.attributes = "ReadOnly,Archive"

  • 屬性數值表示(16位元數值)
  • $file = Get-Item .\x.exe
    $file.attributes = 0x21

Powershell的格式運算子

 此運算子是採用.NET提供的格式化機制,相關格式化資料請參考Formatting Types




數值格式
名稱
"C" or "c"
貨幣
"D" or "d"
十進位
"E" or "e"
指數
"F" or "f"
浮點
"G" or "g"
一般數值
"N" or "n"
Number格式
"P" or "p"
百分比
"X" or "x"
十六進位


日期及時間格式
說明
"d"
不顯示星期的日期.
"D"
顯示星期的日期.
"f"
日期(含星期)及時間(不顯示秒數)
"F"
日期(含星期)及時間(顯示秒數)
"g"
日期及時間(不顯示秒數)
"G"
日期(含星期)及時間(顯示秒數)
"M", "m"
日期 (只顯示月份和日期 )
"O", "o"
Round-trip date/time pattern.
"R", "r"
RFC1123 pattern. (GMT)
"t"
顯示時間(不顯示秒數)
"T"
顯示時間(顯示秒數)
"u"
Universal sortable date/time pattern.
"U"
Universal full date/time pattern.
"Y", "y"
顯示年和月份

    使用方式:
    "{格式}" -f  "欲格式化的值"
    • 單一參數使用方式
    • PS C:\> $VAR_NUM = 1234.1234567
      PS C:\> "{0:C}" -f  $VAR_NUM
      $1,234.12

    • 多參數使用方式
    • PS C:> $VAR_DATE = Get-Date
      PS C:> $VAR_STR = "Today is"
      PS C:> $VAR_STR + $VAR_DATE
      Today is 07/21/2012 15:22:47
      PS C:\> "{0}{1:d}" -f ($VAR_STR,$VAR_DATE)
      Today is 7/21/2012
      {0}對應到$VAR_STR
      {1:d}對應到$VAR_DATE

    Powershell 的比較運算子

     比較運算子有分字串及數字的比較。字串又分為限制大小寫及不限制大小寫的比較。

    若是要比較大小寫時,需在運算子前面加上c字元,如-cle。
      運算子
      說明
      範例
      結果
      -le
      小於或等於
      (字串不限大小寫)
      10 –le 10
      9 –le 10
      “A” –le “a”
      “B” –le “A”
      True
      True
      True
      False
      -lt
      小於
      (字串不限大小寫)
      10 –lt 10
      9 –lt 10
      “A” –lt “a”
      “A” –lt “B”
      False
      True
      False
      True
      -ge
      大於或等於
      (字串不限大小寫)
      10 –ge 10
      11 –ge 10
      “A” –ge “a”
      “A” –ge “B”
      True
      True
      True
      False
      -gt
      大於
      (字串不限大小寫)
      10 –gt 10
      11 –gt 10
      “A” –gt “a”
      “B” –gt “A”
      False
      True
      False
      True
      -eq
      相等
      (字串不限大小寫)
      10 –eq 10
      9 –eq 10
      “A” –eq “a”
      “A” –eq “b”
      True
      False
      True
      False
      -ne
      不相等
      (字串不限大小寫)
      10 –ne 10
      9 –ne 10
      “A” –ne “a”
      “A” –ne “b”
      False
      True
      False
      True
      -like
      相似
      (字串可用*和?替代,並不限大小寫)
      “ABC” –like “a*”
      “ABC” –like “a??”
      “ABC” –like “a?”
      True
      True
      False
      -notlike
      不相似
      (字串可用*和?替代,並不限大小寫)
      “ABC” –notlike “a*”
      “ABC” –notlike “a??”
      “ABC” –notlike “a?”
      False
      False
      True
      -match
      符合
      (字串不限大小寫,並不限大小寫)
      “ABC” –match “A”
      “ABC” –match “a”
      “ABC” –match “[AE]”
      “ABC” –match “AE”
      True
      True
      True
      False
      -notmatch
      不符合
      (字串不限大小寫,並不限大小寫)
      “ABC” –notmatch “A”
      “ABC” –notmatch “a”
      “ABC” –notmatch “[AE]”
      “ABC” –notmatch “AE”
      False
      False
      False
      True
      -contains
      包含
      (運算子的左邊含有右邊的值,並不限大小寫)
      “A”,”B”,”C” –contains “A”
      “a”,”b”,”c” –contains “A”
      “c”,”d”,”e” –contains “A”
      True
      True
      False
      -notcontains
      不包含
      (運算子的左邊含有右邊的值,並不限大小寫)
      “A”,”B”,”C” –contains “A”
      “a”,”b”,”c” –contains “A”
      “c”,”d”,”e” –contains “A”
      False
      False
      True

      Powershell的型別運算子

       

      • -is 用於判別是不是某種型別

      • PS C:\> 12345 -is [int]
        True
        PS C:\> 12345 -is [double]
        False

      • -isnot 用於判別是不是非某種型別

      • PS C:\> 12345 -isnot [int]
        False
        PS C:\> 12345 -isnot [double]
        True

      • -as 將物件轉成指定的型別

      • PS C:\> $StrA = 12345
        PS C:\> $StrA.GetType().Name
        Int32

        PS C:\> $StrA = 12345 -as [string]
        PS C:\> $StrA.GetType().Name
        String

      7/13/2012

      如何保留 Powershell script 執行後的視窗畫面


      Script執行完畢之後,若是沒有設定保留視窗的參數,無法看到輸出的結果。為了解決這個問題,可以增加NoExit的參數保留視窗。

      1. 開啟PowerShell ISE,寫個簡易的程式

      2. 執行test.ps1,這時會發現我們要的結果畫面不會保留.
      3. 解決方法是用個Shortcut,在powershell 後增加NoExit的參數。執行後,就可以出現我們要的結果。