标签: powershell

为什么在PowerShell中此字符串未序列化为简单的JSON字符串

两者均见下方$a,$s是包含文本的字符串,"String"但每个字符串的转换方式与ConvertTo-JSON不同。

为什么不$s | ConvertToJson生产"String"??

PS W:\PowerShell\powowshell> $a="String"
PS W:\PowerShell\powowshell> $a
String
PS W:\PowerShell\powowshell> $a.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


PS W:\PowerShell\powowshell> $a | ConvertTo-Json
"String"


PS W:\PowerShell\powowshell> $s
String
PS W:\PowerShell\powowshell> $s.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


PS W:\PowerShell\powowshell> $s | ConvertTo-Json
{
    "value":  "String",
    "required":  "true"
}
Run Code Online (Sandbox Code Playgroud)

背景故事

$s在parameterValue一个.ps1检查用Get-Help …

powershell json

3
推荐指数
1
解决办法
80
查看次数

Visual Studio代码cmd错误:无法加载,因为此系统上的运行脚本被禁用

在Visual Studio代码内部,我试图从命令行执行script.bat,但出现以下错误:

无法加载文件C:\ Theses_Repo \ train-cnn \ environment \ Scripts \ activate.ps1,因为此系统上的运行脚本已禁用。

读完这篇文章后,我试图以管理员模式运行Visual Studio代码,以为问题出在特权上。但是无论如何都会抛出错误。

powershell command-line visual-studio-code vscode-settings

3
推荐指数
13
解决办法
3666
查看次数

如何在Powershell中使用正则表达式选择“捕获”代码块?

我正在尝试分析许多目录中的许多Powershell脚本,并且我想将任何Catch代码块拉到列表/变量中。

我正在尝试编写正则表达式以选择以下格式的任何块

    Catch 
        {
        write-Host "Function:",$MyInvocation.MyCommand,"Failed with exception:" "Error"
        write-Host "Exception: $_" "Error"
        throw "Exception: $_"
        }

Run Code Online (Sandbox Code Playgroud)
    Catch{
        write-Host "Function:",$MyInvocation.MyCommand,"Failed with exception:" "Error"
        write-Host "Exception: $_" "Error"
        throw "Exception: $_" }
Run Code Online (Sandbox Code Playgroud)
Catch {write-Host "Function:",$MyInvocation.MyCommand,"Failed with exception:" "Error"
        write-Host "Exception: $_" "Error"
        throw "Exception: $_"}

Run Code Online (Sandbox Code Playgroud)

基本上在任何地方都有一个接在{}之后的catch,忽略单词“ Catch”和花括号之间以及花括号之后的换行符,忽略大小写。

我也希望返回{}之间的全部内容,以便我可以对其进行一些其他检查。

我设法弄出的最好的是:


\b(\w*Catch\w*)\b.*\w*{\w.*}
Run Code Online (Sandbox Code Playgroud)

如果全部在一条线上,则将匹配。

我将在powershell中进行此操作,因此将非常感谢.net或powershell类型的正则表达式。

谢谢。

regex powershell parsing

3
推荐指数
1
解决办法
38
查看次数

Powershell获得Azure用户的存在

一年前,我创建了一个脚本,用于输出SkypeFB用户的状态。为此,我使用了Microsoft.Lync.Model.dll:

$client = [Microsoft.Lync.Model.LyncClient]::GetClient()
$contact = $client.ContactManager.GetContactByUri("spiderman@marvel.com")
$availabilityId = $contact.GetContactInformation("Availability")
$activity = $contact.GetContactInformation("Activity")
Run Code Online (Sandbox Code Playgroud)

由于我们所有的员工都不再使用Skype,因此不再需要。他们现在使用MS Teams。有什么办法可以输出完全相同的信息?我试过的

Install-Module AzureAD
Connect-AzureAD -Credential $credential
Get-AzureADUser -ObjectId "spiderman@marvel.com"
Run Code Online (Sandbox Code Playgroud)

此方法有效(我获得有关Azure AD用户的信息),但是找不到任何状态,可用性/活动或状态。有人知道吗?如果您需要任何其他信息,请询问。

powershell azure office365 azure-active-directory microsoft-teams

3
推荐指数
1
解决办法
176
查看次数

如何检查PowerShell开关参数是否缺失或为false

我正在构建一个构建哈希表的PowerShell函数。我正在寻找一种可以使用switch参数指定为不存在,正确或错误的方法。我该如何确定?

我可以通过使用[boolean]参数来解决此问题,但是我没有找到一个很好的解决方案。或者,我也可以使用两个开关参数。

function Invoke-API {
    param(
        [switch]$AddHash
    )

    $requestparams = @{'header'='yes'}

    if ($AddHash) {
        $requestparams.Code = $true
    }
Run Code Online (Sandbox Code Playgroud)

当指定false时如何显示为false,而未指定switch参数时如何显示呢?

powershell

3
推荐指数
2
解决办法
168
查看次数

变量上的++运算符未按ScriptBlock中的预期进行更改

我试图通过在文件中添加基于递增计数器的前缀来重命名文件,例如:

$directory = 'C:\Temp'
[int] $count=71; 

gci $directory | sort -Property LastWriteTime | `
rename-item -newname {"{0}_{1}" -f $count++, $_.Name} -whatif
Run Code Online (Sandbox Code Playgroud)

然而,所有的文件处理是71_和$count在$count++从不增量和文件名前缀相同?为什么?


在此处输入图片说明

powershell scope pipe scriptblock rename-item-cmdlet

3
推荐指数
1
解决办法
73
查看次数

为什么Powershell重定向>>会更改文本内容的格式?

我想使用重定向附加>>或写入>来写入txt文件,但是当我这样做时,我收到了奇怪的格式“ \ x00a \ x00p ...”。

我成功使用了set-content和add-content,为什么它们按预期运行,但>>和>重定向运算符却没有运行?

使用Powershell'cat'以及简单的python打印显示输出。

rocket_brain> new-item test.txt
rocket_brain> "appended using add-content" | add-content test.txt
rocket_brain> cat test.txt

 appended using add-content
Run Code Online (Sandbox Code Playgroud)

但是如果我使用重定向附加>>

rocket_brain> "appended using redirect" >> test.txt
rocket_brain> cat test.txt

 appended using add-content
 a p p e n d e d   u s i n g   r e d i r e c t

Run Code Online (Sandbox Code Playgroud)

简单的python脚本:read_test.py

with open("test.txt", "r") as file:   # open test.txt in readmode
    data = file.readlines()           # append each line to the …
Run Code Online (Sandbox Code Playgroud)

powershell io-redirection

3
推荐指数
1
解决办法
124
查看次数

(日期).ToString不使用指定的分隔符

为什么ToString()方法返回带有“-”而不是指定的“ /”的日期。

PS 11:59  C:\src\t
>((Get-ChildItem 'u.txt').LastWriteTime).ToString('yyyy/MM/dd')
2019-08-09
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
1
解决办法
57
查看次数

是否有更好的方法使用Powershell将标头添加到CSV文件?

有没有更有效的方法将标头添加到CSV文件,而不是对其进行硬编码?

$Importfile = Import-csv $path$filename -header H1, H2, H3, H4, H5, H6, H7 #add headers to csv file

powershell

3
推荐指数
1
解决办法
69
查看次数

为什么我的参数没有传递到函数中?

创建功能时,我的家用笔记本电脑出现异常行为。参数不会传递到函数中。

例:

function Get-Info {
    param (
        $input
    )
    $input | gm
}
Run Code Online (Sandbox Code Playgroud)

使用此代码(Get-Info -input 'test'),我收到以下错误:

gm : You must specify an object for the Get-Member cmdlet.
At line:5 char:14
+     $input | gm
+                   ~~
    + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationException
    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand
Run Code Online (Sandbox Code Playgroud)

我也只是尝试打印带有参数的详细语句,但我只得到一个空行。

为什么参数没有传递到函数中?

powershell function parameter-passing

3
推荐指数
1
解决办法
52
查看次数