html tool

显示标签为“powershell”的博文。显示所有博文
显示标签为“powershell”的博文。显示所有博文

2021年7月20日星期二

转:powershell数字指定长度

 https://www.pstips.net/formatting-numbers-easily.html

[popexizhi:

$a1='{0:n3}' -f $a

echo $a1

]

让我们做一个前置空缺位补足0的例子:

$number = 68
'{0:d7}' -f $number

它将用0补足前面空缺的位数产生一个7位的字符串。后面的”d”是用来控制调整数字前面的位数。

我们再来使用”n”替代”d”限制数字的位数。这次数字后面的”n”用来控制数字后面小数点的位数(尾巴将会四舍五入):

$number = 35553568.67826738
'{0:n1}' -f $number

同样使用“p“格式化百分数(尾巴将会四舍五入):

$number = 0.32562176536
'{0:p0}' -f $number

2021年7月19日星期一

转:powershell 分割符split

 https://docs.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_split?view=powershell-7.1

指定一個或多個要拆分的字符串。如果您發送多個字符串,則使用相同的分隔符規則拆分所有字符串。

例子:

-split "red yellow blue green"
red
yellow
blue
green

<分隔符>

標識子字符串結尾的字符。默認分隔符是空格字符,其中包括空格和不可打印的字符(例如換行符 (` n) 和製表符 (` t))。拆分字符串時,將省略所有子字符串分隔符。例子:

電源外殼
"Lastname:FirstName:Address" -split ":"
Lastname
FirstName
Address

转:powershell ..运算符

 参考: https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.1

范围运算符 ..

表示整数数组中的顺序整数(给定上下限)。

PowerShell
1..10
foreach ($a in 1..$max) {Write-Host $a}

您还可以按相反的顺序创建范围。

PowerShell
10..1
5..-5 | ForEach-Object {Write-Output $_}

2021年5月20日星期四

Get-Process-powershell

参考:

https://forsenergy.com/zh-cn/windowspowershellhelp/html/27a05dbd-4b69-48a3-8d55-b295f6225f15.htm

进程的默认显示为包括以下列的表:

-- Handles:进程打开的句柄数。

-- NPM(K):进程正在使用的非分页内存量,以千字节为单位。

-- PM(K):进程正在使用的可分页的内存量,以千字节为单位。

-- WS(K):进程工作集的大小,以千字节为单位。工作集包括进程最近引用的内存的页面。

-- VM(M):进程正在使用的虚拟内存量,以兆字节为单位。虚拟内存包括磁盘上分页文件中的存储。

-- CPU(s):进程已用于所有处理器的处理器时间量,以秒为单位。

-- ID:进程的进程 ID (PID)。

-- ProcessName:进程的名称。


https://www.starky.ltd/2019/02/26/windows-powershell-cookbook-1/

结构化命令(Cmdlets)

PowerShell 引入了一种名为 Cmdlets 的新型命令。所有的 cmdlets 命令都以 Verb-Noun 的形式命名,如 Get-ProcessGet-ContentStop-Process 等。

1
2
3
4
5
6
7
8
9
PS C:\Users\starky> Get-Process -Name chrome

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
285 36 80040 93808 620 104.61 1384 chrome
201 16 9000 7296 121 0.05 3572 chrome
1061 73 57324 99924 550 145.21 4052 chrome
221 17 8864 7952 131 0.08 5832 chrome
260 30 209528 154748 534 118.55 6408 chrome


win+r 或计划任务中如何运行powershell

 参考: https://www.codenong.com/2035193/

powershell.exe 'C:\my_path\yada_yada

转: Powershell格式化输出当前时间

https://blog.csdn.net/lzk0431/article/details/105001119

Get-Date     #获取当前时间

Get-Date -Format 'yyyy'    #获取当前“年”

Get-Date -Format 'MM'   #获取当前“月”

Get-Date -Format 'dd'   #获取当前“日”

Get-Date -Format 'HH'   #获取当前“时”,24小时制

Get-Date -Format 'hh'   #获取当前“时”,12小时制

Get-Date -Format 'mm'   #获取当前“分”

Get-Date -Format 'ss'   #获取当前“秒”

\

2021年3月4日星期四

关闭powershell的 文件签名

 转: https://blog.csdn.net/baidu_31718835/article/details/113505213

set-ExecutionPolicy RemoteSigned


2020年1月18日星期六

powershell中统计命令使用时间

问题: time 在powershell 如何使用

参考:http://mxii.eu.org/2016/08/05/ways-to-measure-execution-time-in-powershell/


 $startTime=(Get-Date); & tcping -n 100 192.168.100.120 22 ;$Elapsed=(Get-Date)-$startTime; $Elapsed

Pair of [DateTime] objects

Let’s calculate time span as a difference between two moments of time — before and after the command.
> $startTime=(Get-Date); "do stuff"; $Elapsed=(Get-Date)-$startTime; $Elapsed
do stuff


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 9998
TotalDays         : 1,15717592592593E-08
TotalHours        : 2,77722222222222E-07
TotalMinutes      : 1,66633333333333E-05
TotalSeconds      : 0,0009998
TotalMilliseconds : 0,9998

2020年1月13日星期一

powershell init

PowerShell
 - how to run script
     [https://www.windowscentral.com/how-create-and-run-your-first-powershell-script-file-windows-10]
 Set-ExecutionPolicy Restricted -- changed :A
  若要了解计算机上的现用执行策略,请键入:
        get-executionpolicy
    若要在本地计算机上运行您编写的未签名脚本和来自其他用户的签名脚本,请使用以下命令将计算机上的
    执行策略更改为 RemoteSigned:
        set-executionpolicy remotesigned

 - how to exe shell script
     [https://cloud.tencent.com/developer/ask/60300]
一般来说,你调用一个本地命令,其路径中有一个空格,如下所示:
& "c:\some path with spaces\foo.exe" <arguments go here>
这是&后面跟着一个字符串,标识一个命令:cmdlet,函数,本机EXE相对或绝对路径。
一旦你得到这个:
& "c:\some path with spaces\foo.exe"
根据需要引用参数

- loop [https://www.pstips.net/powershell-for-loop.html]
$sum=0
$i=1
for(;$i -le 100;)
{
    $sum+=$i
    $i++
}
$sum

--arrays [http://www.splaybow.com/post/powershell-array.html]
初始化数组
$arr=1,2,3,'a','b','xx';
遍历数组
方法一:
foreach($a in $arr){$a}
foreach对于遍历一个集合(数组也算是一个集合)而言,真是太方便了。
方法二:
for($i=0;$i -lt $arr.Length; $i++){$arr[$i]}
这个就是普通的for循环,从C学到C++,再到Java或C#,一直都这么写,大家应该是比较熟悉的。
方法三:
$i=0; while($i -lt $arr.Length){$arr[$i];$i++}


2018年1月5日星期五

转: Powershell中禁止执行脚本解决办法

Powershell中禁止执行脚本解决办法

http://www.cnblogs.com/shanyou/archive/2011/09/03/2165371.html

PS C:\Windows\system32> get-ExecutionPolicy
Restricted
果然,是被限制。还是在"get-help about_signing"里面可以查到设置命令"set-executionpolicy"
再在帮助中找到"set-executionpolicy"的说明。。似乎是比较建议设置为,至少是被标记为可信任的
-- RemoteSigned: Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.
在PowerShell执行以下:
PS C:\Windows\system32> set-ExecutionPolicy RemoteSigned
执行策略更改
执行策略可以防止您执行不信任的脚本。更改执行策略可能会使您面临 about_Execution_Policies
帮助主题中所述的安全风险。是否要更改执行策略?
[Y] 是(Y)  [N] 否(N)  [S] 挂起(S)  [?] 帮助 (默认值为“Y”): y
PS C:\Windows\system32>

2017年12月19日星期二

PowerShell 设置脚本入参


http://www.pstips.net/powershell-pass-args-to-scripts.html


最好的方式给参数指定名称,输入以下的脚本:
1
2
3
4
param($Directory,$FileName)
"Directory= $Directory"
"FileName=$FileName"
其中param给参数指定名称。
执行脚本:
PS E:> .MyScript.ps1 -Directory $env:windir -FileName config.xml
Directory= C:windows
FileName=config.xml
PS E:> .MyScript.ps1 -FileName config.xml -Directory $env:windir
Directory= C:windows
FileName=config.xml

验证参数

给脚本的参数绑定数据类型,绑定帮助信息。一旦脚本缺少参数,或者输入的参数类型不正确,就提醒用户:
输入脚本:
1
2
3
4
5
6
7
param(
[string]$Name=$(throw "Parameter missing: -name Name") ,
[int]$Age=$(throw "Parameter missing: -age x as number")
)
"Name= $Name"
"Age=$Age"
执行脚本:
PS E:> .MyScript.ps1
Parameter missing: -name Name
所在位置 E:MyScript.ps1:2 字符: 22
+ [string]$Name=$(throw < <<<  "Parameter missing: -name Name") ,     + CategoryInfo          : OperationStopped: (Parameter missing: -name Name:String) [], Runtime    Exception     + FullyQualifiedErrorId : Parameter missing: -name Name PS E:> .MyScript.ps1 -Name mosser
Parameter missing: -age x as number
所在位置 E:MyScript.ps1:3 字符: 18
+ [int]$Age=$(throw < <<<  "Parameter missing: -age x as number")     + CategoryInfo          : OperationStopped: (Parameter missing: -age x as number:String) [], R    untimeException     + FullyQualifiedErrorId : Parameter missing: -age x as number PS E:> .MyScript.ps1 -Name mosser -Age abc
E:MyScript.ps1 : 无法处理对参数“Age”的参数转换。无法将值“abc”转换为类型“System.Int32”。错误:
“输入字符串的格式不正确。”
所在位置 行:1 字符: 33
+ .MyScript.ps1 -Name mosser -Age < <<<  abc     + CategoryInfo          : InvalidData: (:) [MyScript.ps1], ParameterBindin...mationException     + FullyQualifiedErrorId : ParameterArgumentTransformationError,MyScript.ps1 PS E:> .MyScript.ps1 -Name mosser -Age 100
Name= mosser
Age=100

【popexizhi:
 pope用的是有默认参数的如下:
1
2
3
4
5
6
7
param(
    [string]${npldll} = "NPLDLLPATH",
    [string]${npldllnew},
    [string]${nexusExe}="NEXUSEXEPATH",
    [string]${nexusExeNew}
)
"npldllnew= ${npldllnew}"
"nexusExeNew= ${nexusExeNew}"
echo ${npldll}
echo ${npldllnew}
echo ${nexusExe}