Skip to content

Instantly share code, notes, and snippets.

View CN-CODEGOD's full-sized avatar
😏
share my thought

CN-CODEGOD

😏
share my thought
View GitHub Profile
@CN-CODEGOD
CN-CODEGOD / gist:0106b8007928cde15985e4d12687c6a6
Created September 12, 2024 09:48
如何逆推多层级的结构代码
解释:
在代码中可能遇见一种情况需要向外一层层的解剖一个object,但是我们只能从根据内部推出外部.
实例:
我们需要通过一个xml表构建一个psobject
此时我们要构建这个psobject,我们试从xml一层层地剖析
```
([pscustomobject]@{roadcoordinate=@{
> roadcoordinte1=@{x=1
> y=1
> z=1
@CN-CODEGOD
CN-CODEGOD / gist:dd415e0ab67f5f900982bfa227b11c47
Last active December 17, 2024 17:13
HOW to write the save method
Import-xml
using namespace system.xml.linq
class script {
[scriptblock]$scriptblock
[string]$name
#1.add hidden path
hidden [string]$path = "$PSScriptRoot\xml\script.xml"
@CN-CODEGOD
CN-CODEGOD / gist:0e7a583ebe22dd498060c04612345897
Last active November 22, 2024 20:37
使用powershell 添加autocompletion
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
}
@CN-CODEGOD
CN-CODEGOD / gist:7114970e0e9082f888456c03ea16f26b
Created September 15, 2024 06:21
powershell系统环境变量合集转换为数组
# 获取系统环境变量 PATH 并将其拆分为数组
$pathArray = $env:PATH -split ';'
# 输出数组内容
$pathArray
[Parameter(
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public string[] Name
{
get { return this.processNames; }
set { this.processNames = value; }
}
@CN-CODEGOD
CN-CODEGOD / gist:2358dea6164fc608fbb110231f05d47c
Last active December 17, 2024 17:45
powershell AUTO completion
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/',
@CN-CODEGOD
CN-CODEGOD / gist:a4a2987726778c84ba9bfb9b8c1ce621
Created November 18, 2024 09:00
JavaScript via 其他语言
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);
});
@CN-CODEGOD
CN-CODEGOD / gist:6e43d6077e930f71cc70c03f9622ada5
Last active December 17, 2024 09:16
Windows 中文乱码的疑难杂症
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');
@CN-CODEGOD
CN-CODEGOD / gist:7057854cfe45cdd0b81b8b9ed7c78216
Created November 22, 2024 19:53
powershell 参数类型集合
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)]
@CN-CODEGOD
CN-CODEGOD / gist:a42458ba755bec66f64a26e5e8e1640a
Last active December 14, 2024 06:48
Powershell添加系统环境变量
#获取环境变量
$pspath=[System.Environment]::GetEnvironmentVariable('psModulepath','machine')
#添加环境变量
$pspath+=";$home\modules"
#设置环境变量
[System.Environment]::SetEnvironmentVariable('psmodulepath',$pspath,'machine')
#系统环境变量和用户环境变量的区别
系统环境变量: 'machine'
当前用户环境变量:'User'