标签: powershell

新项更改功能返回值

我想编写一个函数,以指定文件夹内的指定名称创建一个文件夹(如果不存在)。事实证明,根据调用New-Item函数返回不同的值。而且我不知道它与New-Item实际的关系。

$folderPath = "C:\tmp"

function CreateFolder([string] $name, [string] $parentFolder) 
{
  $path = "$parentFolder\$name"

  if(!(Test-Path $path))
  {
    New-Item -path $parentFolder -name $name -itemtype Directory
  }

  return $path
}

$FOLDER_NAME = "folder1"

$destination = CreateFolder $FOLDER_NAME $folderPath

echo $destination.GetType()
Run Code Online (Sandbox Code Playgroud)

如果folder1不存在,它将返回:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
Run Code Online (Sandbox Code Playgroud)

除此以外:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
Run Code Online (Sandbox Code Playgroud)

除非作为参数Move-Item支持Object[],否则不会有问题-destination。

echo $destination 返回:

PSPath …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-5.0

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

如果/ ElseIf阻止不使用-or

我正在为我儿子写的剧本有问题.我的意图是提醒他记住他的家务.我刚刚开始做PowerShell,我真的非常喜欢它.我已经买了几本书,还经历了很多其他的帖子.

到目前为止我所得到的是,似乎评估不能正常使用-or (或者我可能会这么做?)

    ## REMINDERS TO DO CHORES ##

$sun = "Sunday"
$mon = "Monday"
$tue = "Tuesday"
$wed = "Wednesday"
$thu = "Thursday"
$fri = "Friday"
$sat = "Saturday"

$today = (get-date).DayOfWeek

$choreVac = "Vacuum the rooms and stairs"
$choreBath = "Clean the Bathroom Including emptying the garbage"
$choreOther = "No Chores Today -- But keep dishes done up"


if($today -eq $mon -or $wed -or $fri) {
msg /time:2500 * "Today is a Chore Day:  your job is …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-ise windows-10 powershell-5.0

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

服务器无法验证请求.验证证书是否有效并与此订阅相关联

尝试编写脚本以在Azure上创建的VM上自动部署Azure.昨天一切都很花花公子.我进行了设置,并且服务正在部署到各自的订阅中.

我的证书区域还有许多Azure管理证书.我已经把它们全部清理干净了,因为除了一个人以外都很老了.然后我重新导入了最新的发布设置文件,希望通过从头开始,一切都会很高兴.

然后我今天登录,运行了我的一个部署,它说:

The server failed to authenticate the request. 
Verify that the certificate is valid and is associated with this subscription.
Run Code Online (Sandbox Code Playgroud)

没问题,只需使用方便的dandy add-azureaccount,它会重新登录,重新运行部署,然后再说:

Set-AzureService : ForbiddenError: The server failed to authenticate the
request. Verify that the certificate is valid and is associated with this
subscription.
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何获得服务器的身份验证?

我在Azure上创建并在VM上安装Teamcity的所有这些自动化.当我尝试直接在VM Powershell上运行脚本时它工作正常但是当我尝试运行Teamcity配置时发生错误,它在PowerShell构建步骤中失败.

It though returns the subscriptions that are there on VM by the command Get-AzureSubscription

[11:31:07][Step 3/3] SubscriptionId :*******

[11:31:07][Step 3/3] SubscriptionName :****

[11:31:07][Step 3/3] Environment : AzureCloud

[11:31:07][Step 3/3] SupportedModes : …

deployment powershell teamcity automation azure

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

Azure CLI内联参数无效

我正在尝试使用以下命令从PowerShell中将参数内联传递给ARM模板:

azure group deployment create -f my_arm_template.json -g myresourcegroup -p '{\"slot\":\"blue\"}' --verbose
Run Code Online (Sandbox Code Playgroud)

并收到错误:

Error converting value "blue" to type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Data.Definitions.DeploymentParameterDefinition'. Path 'properties.parameters.slot'
Run Code Online (Sandbox Code Playgroud)

我正在使用此页面提供的示例:https: //azure.microsoft.com/en-us/documentation/articles/resource-group-template-deploy/

我尝试过没有像示例和各种其他方式一样转义引号,但在尝试验证模板时每次其他尝试都会中断.

更新1:除了Powershell之外,我已经尝试过CMD和相同的结果.

powershell json azure node.js azure-cli

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

如何将带空格的路径传递给脚本

我试图使用PowerShell调用位于包含空格的位置/路径的EXE.当我从命令行调用脚本时,EXE的完整路径不会传递给脚本.关于为什么会发生这种情况的任何想法?

PowerShell脚本内容(Untitled1.ps1)

以下是从命令行调用的整个脚本:

param(
    [string] $ParamExePath
)

function Run-CallThisExe {
    param(
        [string] $ThisExePath
    )

    Write-Host "ThisExePath: " "$ThisExePath"
    start-process -FilePath $ThisExePath
}

write-host "ParamExePath: " $ParamExePath

Run-CallThisExe -ThisExePath "$ParamExePath"
Run Code Online (Sandbox Code Playgroud)

命令行字符串

以下是从PowerShell脚本的父文件夹运行的命令行字符串:

powershell -command .\Untitled1.ps1 -NonInteractive -ParamExePath "C:\path with spaces\myapp.exe"
Run Code Online (Sandbox Code Playgroud)

产量

以下是运行脚本后输出的内容

ParamExePath:  C:\path
ThisExePath:  C:\path

start-process : This command cannot be run due to the error: The system cannot
find the file specified.
At C:\sample\Untitled1.ps1:11 char:5
+     start-process -FilePath $ThisExePath
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ …
Run Code Online (Sandbox Code Playgroud)

powershell command-line

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

在Powershell中创建表

伙计我有一个查询远程计算机服务的脚本.它运行2个嵌套的foreach循环,1个用于计算机列表,内部用于服务列表.我想在二维数组中捕获进程的输出.

The desired output should be like this:

Computer    Service   State   StartupMode
-------------------------------------------
data         data      data     data
data         data      data     data
data         data      data     data
data         data      data     data
Run Code Online (Sandbox Code Playgroud)

我已经获取了数据并将其显示在控制台上,但未能将其存储在多列数组中.哈希表只能接受2列.

arrays powershell multidimensional-array

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

术语"Add-AzureAccount"未被识别为cmdlet,函数,脚本文件的名称

我试图使用这个天蓝色的命令行程序形式C#应用程序,这是我的天蓝色虚拟机中托管的控制台作业.这个作业自动运行一天一次,但我的azure命令行程无法执行.昨天工作正常,但一天后它无法正常工作,请帮助我.☺

注意:这里我正在做一些管理任务,我正在使用组织帐户.但它无法添加帐户其他命令行程无法正常工作.

c# powershell azure powershell-remoting powershell-3.0

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

Powershell错误“无法设置”属性”,因为仅字符串可以用作设置XmlNode属性的值

在将值分配给XML的SOAP调用时遇到问题。该值在变量中定义,但Powershell不断返回此错误:

“无法设置“属性”,因为只能将字符串用作设置XmlNode属性的值”

我的变量定义为字符串: [string]$AvidDisplayNameMinusTransfer

这是带有变量的SOAP调用:

$soap = [xml]@'

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://avid.com/interplay/ws/assets/types">
   <soapenv:Header>
      <typ:UserCredentials>
         <typ:Username>***</typ:Username>
         <typ:Password>***</typ:Password>
      </typ:UserCredentials>
   </soapenv:Header>
   <soapenv:Body>
      <typ:Search>
         <typ:InterplayPathURI>interplay://AvidEng103/LTW</typ:InterplayPathURI>
         <typ:SearchGroup Operator="AND">
            <!--Zero or more repetitions:-->
            <typ:AttributeCondition Condition="EQUALS">
               <typ:Attribute Group="USER" Name="Display Name"></typ:Attribute>
            </typ:AttributeCondition>
         </typ:SearchGroup>
         <typ:ReturnAttributes>
            <typ:Attribute Group="SYSTEM" Name="MOB ID"></typ:Attribute>
         </typ:ReturnAttributes>
      </typ:Search>
   </soapenv:Body>
</soapenv:Envelope>

'@

$soap.Envelope.Body.Search.SearchGroup.AttributeCondition.Attribute = $AvidDisplayNameMinusTransfer
Run Code Online (Sandbox Code Playgroud)

让我知道是否有人需要我的整个代码来提供更好的上下文。

powershell soap

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

当服务器返回状态代码400时,使用powershell检索响应内容

所以我通过powershell调用一个休息Web服务,如果你试图发布已经处理过的数据,它会响应一个400状态代码,以及状态代码它还会返回一个JSON响应:

{
    "statusCode": 400,
    "errorCode": "REQUEST_VALIDATION_ERROR",
    "message": "validation failed",
    "developerMessage": " MessageId-xxx already processed. No Need to process again."
}
Run Code Online (Sandbox Code Playgroud)

所以我尝试用一​​些powershell调用它并接收400状态代码并使用try/catch来处理这个以获取异常,但是响应中没有内容,我可以获得响应头,状态代码,状态消息等,但是我根本找不到访问JSON响应的方法,这里是我正在使用的powershell代码的格式:

$url = "https://example.com/webservice/status"
$cert = Get-ChildItem Cert:\CurrentUser\My\certthumbprintxxxxx
$headers = @{"tokenId" = "tokenxxxxx"}

$body = 
@"
{
...
 JSON data...
...
}
"@

try 
{
    $response = Invoke-WebRequest $url -Certificate $cert -Headers $headers -Method Post -Body $body
    }
catch
{
    $_.Exception.response
    }
Run Code Online (Sandbox Code Playgroud)

当我发送$_.Exception.response给Get-Member时,我可以看到没有内容属性.如何使用PowerShell访问响应中的消息?

powershell web-services

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

Azure Web App:发布前删除所有文件

使用以下Powershell脚本发布到我们的Azure Web App时,我们经常遇到问题,即先前发布的文件会导致运行时错误.

param($websiteName, $packOutput)

$website = Get-AzureWebsite -Name $websiteName

# get the scm url to use with MSDeploy.  By default this will be the second in the array
$msdeployurl = $website.EnabledHostNames[1]

$publishProperties = @{'WebPublishMethod'='MSDeploy';
                        'MSDeployServiceUrl'=$msdeployurl;
                        'DeployIisAppPath'=$website.Name;
                        'Username'=$website.PublishingUsername;
                        'Password'=$website.PublishingPassword}

Write-Output "Stopping web app..."
Stop-AzureWebsite -Name $websiteName

Write-Output "Publishing web app..."
$publishScript = "${env:ProgramFiles(x86)}\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web Tools\Publish\Scripts\default-publish.ps1"

Write-Output "Starting web app..."
Start-AzureWebsite -Name $websiteName

. $publishScript -publishProperties $publishProperties  -packOutput $packOutput
Run Code Online (Sandbox Code Playgroud)

如何在发布之前删除所有现有文件?

请注意,我们不能使用插槽,因为这需要标准实例,这对我们的CI环境而言过于昂贵.

powershell azure msdeploy azure-web-sites azure-powershell

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