标签: powershell

如何在 PowerShell 中从 Get-ChildItem 中排除文件和文件夹?

我制作了一个 PowerShell 脚本,该脚本通过 md5 检查运行 robocopy。

它工作正常,但如果我尝试排除某些目录或文件,robocopy 会处理排除,而比较哈希的脚本的 MD5 部分不起作用,会返回一些错误,因为源的文件/哈希比目标多。 。

我可能已经尝试过我在这里和互联网上找到的所有方法!我无法从路径中排除目录和/或文件!

以下是我到目前为止所做的事情。在此模式下,md5 复制有效(不排除):

$Source = "F:\"

$IgnoreDir = @(
    $Source + '$RECYCLE.BIN'
    $Source + "System Volume Information"
    $Source + "VMs"
)   
$IgnoreFile = @(
    $Source + "SHDrive.vmdk"
    $Source + "SHDrive-flat.vmdk"
)
$Ignored = $IgnoreDir + $IgnoreFile
Run Code Online (Sandbox Code Playgroud)

机器人复制:

Robocopy.exe /R:1 /W:0 $Source $Dest /E /V /TEE /XD $IgnoreDir /XF $IgnoreFile /LOG:$LogDir\RBCY_MD5_F.txt
Run Code Online (Sandbox Code Playgroud)

MD5:

$SourceHash = Get-ChildItem "$Source\*.*" -Recurse -Force -Exclude $Ignored | Where-Object {!$_.psiscontainer } | Get-FileHash
$SourceHash | Select-Object "Hash", …
Run Code Online (Sandbox Code Playgroud)

powershell get-childitem get-filehash

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

如何执行变量中的powershell代码

假设我将以下 powershell 代码存储在文件中:

## file1.ps1

$myvar = "i am here"
if ($myvar -ne $null) {
    "($myvar) variable is Full"
} else {
    "($myvar) variable is Empty"
}
Run Code Online (Sandbox Code Playgroud)

然后这段代码被存储在一个变量中:

$code_in_var = cat file1.ps1
Run Code Online (Sandbox Code Playgroud)

如何通过管道执行变量中的代码?

我已经尝试过以下方法:

PS C:\Mrm> $code_in_var = cat file1.ps1
PS C:\Mrm>
PS C:\Mrm> cat file1.ps1 | powershell -
PS C:\Mrm> 
PS C:\Mrm> cat file1.ps1 | Invoke-expression
Invoke-expression ; At line:1 char:23
+ if ($myvar -ne $null) {
+            ~
Missing closing '}' in statement bllock or type definition
At …
Run Code Online (Sandbox Code Playgroud)

powershell

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

如何在 Windows 中使用“dir”命令根据上次更新时间对文件进行排序

我在 Windows 10 的命令行提示符下使用“dir”命令。我尝试了以下操作:

c:\my_folder> dir /s /b
Run Code Online (Sandbox Code Playgroud)

但是,上面的命令并没有根据文件的最后更新时间来显示文件。

windows powershell command-line

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

Invoke-SQLCMD 无法在 PowerShell 中运行多个 SQL 语句

我无法从 Invoke-sqlcmd 运行 2 个 SELECT 语句。

$sql ="
select SERVERPROPERTY('ProductVersion') Version
select SERVERPROPERTY('ProductLevel') ServicePack, SERVERPROPERTY('edition') Edition"
    
$server = 'SomeServerName'    
Invoke-Sqlcmd -ServerInstance $server -Query $sql 
Run Code Online (Sandbox Code Playgroud)

只有第一个 TSQL 语句返回结果。

注意:我可以重写 TSQL 来生成一条语句。这不是重点。原始脚本有多个 TSQL 命令。另外,我可以为每个语句执行单独的 invoke-sqlcmd 调用。但是,我试图了解这是否是 invoke-sqlcmd 的限制。

这是预期的行为吗?

太感谢了。

sql-server powershell

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

PowerShell 脚本添加“--prod”参数来构建 Angular CLI 项目

我正在创建 PowerShell 脚本来自动构建 Angular CLI 项目。当我在 .ps1 文件中使用此命令时:

Start-Process -FilePath ng build -WorkingDirectory D:\pathToAngularProject -Wait
Run Code Online (Sandbox Code Playgroud)

它工作完美,但创建的 JavaScript 构建并不丑陋。为了能够创建生产版本,我需要添加“--prod”参数。但是当我更改上面的示例时:

Start-Process -FilePath ng build -ArgumentList '--prod' -WorkingDirectory D:\pathToAngularProject -Wait
Run Code Online (Sandbox Code Playgroud)

我收到错误: 在此输入图像描述

有谁建议我如何在 PowerShell 脚本中向“ng build”命令添加参数?

powershell angular-cli angular

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

Powershell 创建新的注册表值

我想在此路径“Computer\\HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\16.0\\Excel\\Security”中创建一个新的注册表值\n它应该是安全密钥内的一个新的“DWORD” “VBAWarnings”和值应该是一。

\n

我尝试过以下方法:

\n
New-ItemProperty \xe2\x80\x93Path "HKCU:\\Software\\Microsoft\\Office\\16.0\\Outlook\\Security" -Value "VBAWarnings"  -PropertyType "DWORD"\n
Run Code Online (Sandbox Code Playgroud)\n

但是它没有创建任何内容,并且不确定我收到的每个错误消息是什么“

\n
((Get-Process MicrosoftEdgeCP -ErrorA.ps1:1 char:124\n+ ... ce\\16.0\\Outlook\\Security" -Value "VBAWarnings"  -PropertyType "DWORD"\n+                                                                         ~\nThe string is missing the terminator: ".\n    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException\n    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString"\n
Run Code Online (Sandbox Code Playgroud)\n

powershell

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

我可以在 powershell 脚本中写入警告而末尾不换行吗?

我想在 PowerShell 中打印一条警告作为提示,然后阅读同一行的答案。问题是Write-Warning在消息末尾打印换行符,而另一种选择,Read-Host -Prompt,不会将提示打印到警告流(或以黄色打印)。我见过Write-Warning -WarningAction Inquire,但我认为这有点冗长并且提供了我不想要的选项。

我做过的最好的事情是:

  $warningMsg= "Something is wrong. Do you want to continue anyway Y/N? [Y]:"
  Write-Host -ForegroundColor yellow -NoNewline $warningMsg
  $cont = Read-Host
Run Code Online (Sandbox Code Playgroud)

这非常有效——打印黄色提示,然后读取同一行的输入——但我想知道我看到的反对使用的警告Write-Host,是否更适合找出某种打印到警告流的方法没有换行符。有没有办法做到这一点?我注意到这Write-Host似乎是一个写入信息流的包装器,但我没有看到任何方法可以在没有新行的情况下写入警告([Console]::Warning.WriteLine()例如,不存在)。

powershell powershell-7.0

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

使用 PowerShell 从 Azure 应用程序配置获取键值对

AZ 模块中没有任何 cmdlet 可用于从 Azure 应用程序配置获取键值对。除了 AZ CLI 之外还有其他选项吗?有没有办法通过 PowerShell 与服务交互?

powershell azure azure-app-configuration

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

Azure CLI Rest 编码问题(德语变音符号)

我使用该命令在 Azure API 管理中创建用户,但遇到了包含德语元音变音az rest的用户名问题(例如 \xc3\xa4、\xc3\xb6、\xc3\xbc)。该脚本将创建用户,但名称包含无效字符。这里有一个例子:

\n

在此输入图像描述

\n

这就是我创建用户的方式。由于一些带有双引号的 PowerShell / AZ 问题,我确实将用户写入文件中,并将其传递给 az cli:

\n
@{\n    properties = @{\n      confirmation = "invite"\n      firstName    = "Max"\n      lastName     = "M\xc3\xbcstermann"\n      email        = "max.mustermann@stackoverflow.com"\n      appType      = "developerPortal"\n    }\n  } | ConvertTo-Json -Compress | Out-File $tmpFile -Encoding UTF8\n
Run Code Online (Sandbox Code Playgroud)\n

这就是我将文件传递到 cli 的方式:

\n
az rest --method put --uri $uri --body @$tmpFile\n
Run Code Online (Sandbox Code Playgroud)\n

文件本身看起来不错:\n在此输入图像描述

\n

我已经尝试添加charset=utf-8- 但它不能解决问题。有任何想法吗?

\n

rest powershell encoding json azure

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

如何从 Powershell 调用 Win32 API 函数?

我需要使用 PowerShell 从 Win 32 API 中的 Imagehlp.dll 调用 MapFileAndCheckSumA 函数。我尝试遵循一些类似的在线教程,例如: https: //devblogs.microsoft.com/scripting/use-powershell-to-interact-with-the-windows-api-part-1/ 我尝试过使用 Add-Type cmdlet 在 PowerShell 中编译 C# 代码。我写的代码如下。

Add-Type -TypeDefinition @"
 using System;
 using System.Diagnostics;
 using System.Runtime.InteropServices;

 public static class Imagehlp
 {
     [DllImport("imagehlp.dll", CharSet=CharSet.Auto)]
         public static extern bool MapFileAndCheckSumA(
             [MarshalAs(UnmanagedType.LPStr)] string Filename,
             UIntPtr HeaderSum,
             UIntPtr CheckSum);
 }
"@

 $x = [UIntPtr]::new(5)
 $y = [UIntPtr]::new(5)
 [Imagehlp]::MapFileAndCheckSumA("C:\Program Files\Internet Explorer\iexplore.exe", $x,$y)
Run Code Online (Sandbox Code Playgroud)

但是,当我执行代码的最后一行时,出现以下异常。

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory …
Run Code Online (Sandbox Code Playgroud)

.net c# powershell winapi

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