在写powershell脚本的时候,经常会用到一些常用函数function,如果这些function比较多,就会导致你新写的脚本,比较长,比较杂。 所以可以从别的文件导入这些共同的function。
假设公共function方在 D:\common_functions.ps1 里面, 代码如下 | function escape_wildcards_to____string([string] $s) { | | $result = $s -replace '\[','___' | | $result = $result -replace '\]','___' | | $result = $result -replace '\*','___' | | $result = $result -replace '\?','___' | | $result = $result -replace '\/','___' | | $result = $result -replace '\\','___' | | $result = $result -replace '\:','___' | | return $result | | } | | function replace_to_dot_string([string] $s) { | | $result = $s -replace ':','.' | | $result = $result -replace '&','.' | | $result = $result -replace '\?','.' | | $result = $result -replace '/','.' | | return $result | | } | | | | function mkdir_if_exists([string] $s) { | | if(!(Test-Path $s)) { | | mkdir $s | | Write-Host "mkdir -Path = "$s -ForegroundColor Yellow | | } else { | | Write-Host "mkdir -Path = "$s "already exists!" -ForegroundColor Green | | } | | }COPY |
然后,你在新的ps1脚本里,按照如下导入即可: | $self_filename=$MyInvocation.MyCommand.Name | | $host.ui.RawUI.WindowTitle="$self_filename" | | set-executionpolicy remotesigned | | | | . "D:\common_functions.ps1"COPY |
|