我有一个批处理文件 Install.bat,它调用 GetVersion.ps1 powershell 脚本。这两个脚本位于同一个文件夹(C:\Install_Media)中,我通过获取批处理文件所在的目录(使用 %~dp0%)来调用 powershell 脚本。
如果这些文件所在的路径中没有空格,则以下代码效果很好。如果路径中有空格,则 shellscript 不会被执行(例如 C:\Install Media)。脚本停止说术语“C:\Install”不被识别为 cmdlet、函数、脚本的名称。
@ECHO OFF
set SRC_DIR=%~dp0%
Powershell set-executionPolicy remotesigned
Powershell %SRC_DIR%\GetSLMClientVersion.ps1
Powershell set-executionPolicy restricted
Run Code Online (Sandbox Code Playgroud)
我的 powershell 脚本中有这段代码,它在特殊字符部分表现不佳。
$request = 'http://151.80.109.18:8082/vrageremote/v1/session/players'
$a = Invoke-WebRequest -ContentType "application/json; charset=utf-8" $request |
ConvertFrom-Json |
Select -expand Data |
Select -expand players |
Select displayName, factionTag | Out-file "$scriptPath\getFactionTag.txt"
Run Code Online (Sandbox Code Playgroud)
在我的输出文件中,我只得到 '????' 对于任何特殊字符。有谁知道如何让它在我的输出文件中显示特殊字符?
我正在创建一个 arm 模板来在 ADF 中部署数据集,为此我需要根据我的输入文件使用新的键值对更新现有的 json 文件。如何使用 powershell 向 json 文件添加新的键值对。对此的任何帮助都非常感谢..
如果正在使用“添加成员”,它将使用新的“键”和“值”更新结构中的所有属性,如下所示。但我希望在另一个值对之后添加新的键和值,就像我在远处显示的那样下面用“需要添加这个”突出显示的代码
{
"name": "VIN",
"type": "String"
"newkey1" : "newvalue1"
"newkey2" : "newvalue2"
},
{
"name": "MAKE",
"type": "String"
"newkey1" : "newvalue1"
"newkey2" : "newvalue2"
},
Run Code Online (Sandbox Code Playgroud)
我的代码应该看起来像这样..“需要添加这个”是我打算在每个循环中添加的键值对,只要我有来自另一个文本文件的输入。
{
"name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
"type": "Microsoft.DataFactory/factories/datasets",
"apiVersion": "2018-06-01",
"properties": {
"linkedServiceName": {
"referenceName": "AzureDataLakeStore1",
"type": "LinkedServiceReference"
},
"annotations": [],
"type": "AzureDataLakeStoreFile",
"structure": [
{
"name": "VIN",
"type": "String"
},
{
"name": "MAKE",
"type": "String"
},
{
"Need to add this": "Need to add …Run Code Online (Sandbox Code Playgroud) 一天多以来,我一直在处理 ARM 模板遇到的一个问题,但似乎卡住了,因此请询问 SO,以防万一有人可以提供帮助。
为了描述这个问题,我有一个现有的 Azure Key Vault 设置,并希望向这个资源组添加一些访问策略。仅供参考,以下是用于添加 Key Vault 访问策略的 ARM 模板参考:
https://docs.microsoft.com/en-gb/azure/templates/Microsoft.KeyVault/2018-02-14/vaults/accessPolicies
我有许多用户希望为其分配相同的权限,并且此用户列表不太可能经常更改,因此我希望在模板本身中对引用这些用户的 objectId 数组进行硬编码,然后使用“复制"创建多重访问策略的功能。本质上,我希望最终结果接近于此,其中访问策略之间唯一更改的值是标识用户的 objectID:
{
"name": "TestKeyVault/add",
"type": "Microsoft.KeyVault/vaults/accessPolicies",
"apiVersion": "2018-02-14",
"properties": {
"accessPolicies": [
{
"tenantId": "tenantIDStringGoesHere",
"objectId": "guidForUser1GoesHere",
"permissions": {
"keys": ["List"],
"secrets": ["List"],
"certificates": ["List"]
}
},
{
"tenantId": "tenantIDStringGoesHere",
"objectId": "guidForUser2GoesHere",
"permissions": {
"keys": ["List"],
"secrets": ["List"],
"certificates": ["List"]
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我想用循环来解决这个问题而不是手动复制访问策略的原因是我相信它会使维护更容易,因为可以通过从数组中添加或删除值来处理人员更改,而不必复制或删除大部分文本。
我试图理解此处概述的“复制”语法,但我还没有找到适合我的用例的正确组合:
https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-multiple
我得到的最接近的是以下内容:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"keyVaultName": {
"type": "string",
"metadata": { …Run Code Online (Sandbox Code Playgroud) 在我的一个 powershell 函数中,我想收集用户的输入,但首先我需要给出一些说明。我想在控制台中用不同的颜色打印一两行。
function myFunction(){
param(
[string]$directions = $(read-host "Please answer the questions according to your opinion`nYour answers must be Star Wars-based." -foregroundcolor "Magenta"),
[string]$robot = $(read-host "What is your favourite robot" -foregroundcolor "Yellow"),
[string]$spaceship = $(read-host "What is your favourite spaceship" -foregroundcolor "Green")
)
write-host "Favourite Robot = " + $robot
write-host "Favourite Spaceship = " + $spaceship
}
#call the function
myFunction
Run Code Online (Sandbox Code Playgroud)
在上面的函数中,我有一个换行符来保持不同级别的方向,但我希望文本的第一行是一种颜色,第二行是另一种颜色。
此外,-foregroundcolor在这里不起作用 - 它只是按字面打印。
我不能write-host在param声明之前放一个,或者我会把方向放在那里(我知道如何用多种颜色来做到这一点)。
我将以下内容作为参数传递给 powershell(w7 上的 v4):
-debugWrite -fileName SettingsFile -jsonContent { "c": "some setting", "d": "unknown", "b": "some thing", "a": 1 }
Run Code Online (Sandbox Code Playgroud)
但是 PS 被 JSON 挂断了。我试过分隔 \double-quotes\ 并将 -jsonContent 之后的所有内容放在“单引号”中,但无济于事。
这是运行 PS 的 Windows 7 (PS4) 环境:
注意:“...”混淆指的是同一个目录。IOW,所有文件都位于同一目录中。
运行一个批处理文件,开始整个事情:
"C:\...\script.bat" > "C:\...\script-output.txt" 2>&1
Run Code Online (Sandbox Code Playgroud)
这会运行script.bat并输出到script-output.txt. script.bat是一个长的 1-liner:
%WINDIR%\sysnative\windowspowershell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\...\customscript.PS1" --% -fileName DropFileToCreate -jsonContent "{ "c": "some setting", "d": "unknown", "b": "some thing", "a": 1 }" -debugWrite
Run Code Online (Sandbox Code Playgroud)
传奇:
DropFileToCreate - 传递给 PS 脚本的文件名,用于在同一目录中创建文件。
-jsonContent- …
我有一个创建计划任务的脚本,我需要它在“工作站解锁”上运行,但我找不到它的语法。
我的意图是做类似的事情:
(New-ScheduledTaskTrigger -whatDoiWriteHere).
Run Code Online (Sandbox Code Playgroud) 我有一个需要在系统上安装 openssl 的脚本。我想检查一下它是否已安装。我已经考虑过使用,test-path但是因为这个脚本将在许多计算机上,所以无法知道用户安装 openssl 的位置或者它是否在系统路径中。
有没有办法做类似的事情test-command openssl(我知道那不存在)并获得错误级别或类似的信息以在 powershell 中返回?
非常感谢!
我已经按照https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-1.0.0 上的说明安装了 Az PowerShell 模块和 .NET Framework 4.7.2但是客户端仍然找不到模块!
PS C:\Scripts> get-module Az
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0.1 Az
PS C:\Scripts> get-module
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0.1 Az
Script 1.0.0 Az.Accounts {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Conn...
Script 1.0.0 Az.Aks {Get-AzAks, Import-AzAksCredential, New-AzAks, Remove-AzAk...
Script 1.0.0 Az.AnalysisServices {Add-AzAnalysisServicesAccount, Export-AzAnalysisServicesI...
Script 1.0.0 Az.ApiManagement {Add-AzApiManagementApiToProduct, Add-AzApiManagementProdu...
Script 1.0.0 Az.ApplicationInsights {Get-AzApplicationInsights, Get-AzApplicationInsightsApiKe...
Script 1.0.0 Az.Automation {Export-AzAutomationDscConfiguration, Export-AzAutomationD...
Script 1.0.0 Az.Batch {Disable-AzBatchAutoScale, Disable-AzBatchComputeNodeSched...
Script 1.0.0 …Run Code Online (Sandbox Code Playgroud) 我有一个大问题。对于学校俱乐部,我必须编写一个自动发送电子邮件的脚本。我决定为此使用 PowerShell。
我的代码:
Send-MailMessage –To "email@outlook.com" –Subject "Test E-Mail" –Body "FIRST EMAIL WITH POWERSHELL" –SmtpServer "smtp.gmail.com" –From "email@gmail.com"
Run Code Online (Sandbox Code Playgroud)
我的错误代码:
Send-MailMessage:SMTP 服务器需要安全连接或客户端尚未通过身份验证。服务器响应为:5.7.0 必须首先发出 STARTTLS 命令。o3sm51888528wrs.30 - gsmtp In line: 1 character: 1 + Send-MailMessage -To "email@outlook.com" 主题 "Test E-Mail" -Bod ... + ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: InvalidOperation: (System.Net .Mail.SmtpClient: SmtpClient) [Send-MailMessage], SmtpException +fullyQualifiedErrorId: SmtpException, Microsoft.PowerShell.Commands.SendMailMessage
如果我写
–SmtpServer "gsmtp.gmail.com"
Run Code Online (Sandbox Code Playgroud)
错误代码是:
Send-MailMessage : 无法建立到远程服务器的连接。In line:1 character:1 + Send-MailMessage -To "email@outlook.com"; - 主题“测试电子邮件”;- 博德。. . + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) …