This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 解释: | |
| 在代码中可能遇见一种情况需要向外一层层的解剖一个object,但是我们只能从根据内部推出外部. | |
| 实例: | |
| 我们需要通过一个xml表构建一个psobject | |
| 此时我们要构建这个psobject,我们试从xml一层层地剖析 | |
| ``` | |
| ([pscustomobject]@{roadcoordinate=@{ | |
| > roadcoordinte1=@{x=1 | |
| > y=1 | |
| > z=1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Import-xml | |
| using namespace system.xml.linq | |
| class script { | |
| [scriptblock]$scriptblock | |
| [string]$name | |
| #1.add hidden path | |
| hidden [string]$path = "$PSScriptRoot\xml\script.xml" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1.Dynamic ValidateSet values using classes | |
| Class SoundNames : System.Management.Automation.IValidateSetValuesGenerator { | |
| [string[]] GetValidValues() { | |
| $SoundPaths = '/System/Library/Sounds/', | |
| '/Library/Sounds', | |
| '~/Library/Sounds' | |
| $SoundNames = ForEach ($SoundPath in $SoundPaths) { | |
| If (Test-Path $SoundPath) { | |
| (Get-ChildItem $SoundPath).BaseName | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 获取系统环境变量 PATH 并将其拆分为数组 | |
| $pathArray = $env:PATH -split ';' | |
| # 输出数组内容 | |
| $pathArray |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Parameter( | |
| Position = 0, | |
| ValueFromPipeline = true, | |
| ValueFromPipelineByPropertyName = true)] | |
| [ValidateNotNullOrEmpty] | |
| public string[] Name | |
| { | |
| get { return this.processNames; } | |
| set { this.processNames = value; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1.The ValidateSet attribute specifies a set of valid values for a parameter or variable and enables tab completion. PowerShell generates an error if a parameter or variable value doesn't match a value in the set. In the following example, the value of the Fruit parameter can only be Apple, Banana, or Pear. | |
| [ValidateSet('Apple', 'Banana', 'Pear')] | |
| 2.You can use a Class to dynamically generate the values for ValidateSet at runtime. In the following example, the valid values for the variable $Sound are generated via a Class named SoundNames that checks three filesystem paths for available sound files: | |
| Class SoundNames : System.Management.Automation.IValidateSetValuesGenerator { | |
| [string[]] GetValidValues() { | |
| $SoundPaths = '/System/Library/Sounds/', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1.via bat: | |
| const { exec, spawn } = require('node:child_process'); | |
| exec('my.bat', (err, stdout, stderr) => { | |
| if (err) { | |
| console.error(err); | |
| return; | |
| } | |
| console.log(stdout); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| windowS常见encoding:有 cp936,UTF-8 | |
| 确保你和来源字体编码相同 | |
| powershell一般使用的是utf-8,确保你来源和终端encoding相同即可 | |
| 如果来源JavaScript 可以用 | |
| iconv-lite 模组进行encoding 转码 | |
| 实例: | |
| cp936:utf-8 | |
| const { exec } = require('node:child_process'); | |
| const path = require('node:path'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1.parameterset | |
| nction Measure-Lines { | |
| [CmdletBinding(DefaultParameterSetName = 'Path')] | |
| param ( | |
| [Parameter(Mandatory, ParameterSetName = 'Path', Position = 0)] | |
| [Parameter(Mandatory, ParameterSetName = 'PathAll', Position = 0)] | |
| [string[]]$Path, | |
| [Parameter(Mandatory, ParameterSetName = 'LiteralPathAll', ValueFromPipeline)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #获取环境变量 | |
| $pspath=[System.Environment]::GetEnvironmentVariable('psModulepath','machine') | |
| #添加环境变量 | |
| $pspath+=";$home\modules" | |
| #设置环境变量 | |
| [System.Environment]::SetEnvironmentVariable('psmodulepath',$pspath,'machine') | |
| #系统环境变量和用户环境变量的区别 | |
| 系统环境变量: 'machine' | |
| 当前用户环境变量:'User' |
OlderNewer