标签: powershell

|之间的差异 和$

任何人都可以向我解释我所看到的差异,或者使用|一个命令将一个命令传递给另一个命令,或者用$一种不同的方式"管道"它(很抱歉不确定该用途$是否实际上考虑了管道).

所以...这有效:

Get-Mailbox -ResultSize Unlimited | 
    where { $_.RecipientTypeDetails.tostring() -eq "SharedMailbox" } | 
    Get-MailboxPermission
Run Code Online (Sandbox Code Playgroud)

哪个很棒,但是因为我想在上面的命令之后放置另一个命令,Get-MailboxPermission然后尝试使用它:

$Mailbox = Get-Mailbox -ResultSize Unlimited | 
           where { $_.RecipientTypeDetails.tostring() -eq "SharedMailbox" }

Get-MailboxStatistics -Identity $Mailbox |
    where { $_.IsInherited.tostring() -eq "False" }
Run Code Online (Sandbox Code Playgroud)

它导致我得到这个错误:

无法处理参数'Identity'上的参数转换.无法将类型为"System.Collections.ArrayList"的"System.Collections.ArrayList"值转换为"Microsoft.Exchange.Configuration.Tasks.GeneralMailboxOrMailUserIdParameter".

肯定使用|或者$是相同的,因为它将结果传递给下一个命令,或者我完全错了?

powershell exchange-server pipe

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

DSC自定义资源依赖关系

在DSC中,我如何创建自定义模块(PowerShell 5 DSC类或MOF +脚本)并表示它需要/依赖于另一个DSC资源(如PowerShell Gallery中的xBlah).

在chef中,我可以将这些依赖项放在metadata.rb文件中以表达inter-cookbook依赖项.有没有办法在DSC中表达资源间的依赖关系?

powershell dsc powershell-5.0

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

"无法找到创建服务的路径的一部分

我尝试使用以下方法创建Azure云服务:

创建云服务

New-AzureServiceProject -ServiceName 'service-lift-and-shift' -Verbose
Run Code Online (Sandbox Code Playgroud)

我使用Azure SDK 2.9,它给出以下错误:

ServiceName: service-lift-and-shift
New-AzureServiceProject : Could not find a part of the path 'C:\Program Files (x86)\Microsoft 
SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.Profile\Resources\Scaffolding\General\scaffold.xml'.
At line:1 char:1
+ New-AzureServiceProject
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureServiceProject], DirectoryNotFoundException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.CloudService.Development.Scaffolding.NewAzureServiceProjectCommand

powershell azure

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

Powershell循环通过格式表

我有一个问题.我创建了一个包含文件名,源目录和目标目录的格式表.现在我尝试用foreach遍历表格.在这个循环中,我想将文件从源目录移动到目标目录.我的问题是从行中获取项目.

这是我的示例代码:

cls
$MovePathSource = "C:\Users\user\Desktop\sourcefolder"
$MovePathDestination = "C:\Users\user\Desktop\destinationfolder"

$filetypes = @("*.llla" , "html")
$table = dir $MovePathSource -Recurse -Include $filetypes | Format-Table@{Expression={$_.Name};Label="Filename"},@{Expression={($_.DirectoryName)};Label="Sourcepath"},@{Expression={($_.DirectoryName).Replace($MovePathSource,$MovePathDestination)};Label="Destinationpath"}

$table

foreach ($row in $table)
{
write-host "$row.Sourcepath"
#Move-Item -Path ($row.Sourcepath + "\" + $row.Filename) -Destination $row.Destinationpath
}
Run Code Online (Sandbox Code Playgroud)

powershell foreach formattable

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

powershell if语句奇怪的行为

所以这是一个愚蠢的问题.有人可以解释这有什么意义吗?我真的不知道还有什么要问的.为什么if语句会改变变量的值?

PS C:\Users\HD2> $format = 6

PS C:\Users\HD2> $format
6

PS C:\Users\HD2> if($format = 1){write-host "woo"}
woo

PS C:\Users\HD2> $format
1
Run Code Online (Sandbox Code Playgroud)

powershell

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

如何在PowerShell别名中运行多个命令

我正在尝试在$profile文件中为PowerShell添加别名。

Set-Alias regrunt grunt;g aa;g cm 'regrunted';g ps;
Run Code Online (Sandbox Code Playgroud)

当我运行时regrunt,仅运行第一个命令。如何使该别名运行所有命令?

PS:请不要发表有关“不提交缩小文件”的评论,我们都通过了此评论。

windows powershell

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

从PowerShell ISE调用时,无法在异步C#代码中写入控制台

我有一个PowerShell脚本需要调用C#才能正常工作,其中一些是异步的.在调试一些仅在从PowerShell调用C#代码时出现的错误时,我注意到了一些我没想到的东西.在C#代码中,如果我调用Console.WriteLine它,则按预期写入PowerShell控制台,除非我使用的是PowerShell ISE,并且在我在异步函数中进行等待后调用它.

这是一个显示问题的示例脚本:

$source = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

public class TestClass
{   
    public static int TestWrapper()
    {
        var task = TestAsync();
        task.Wait();
        return task.Result;
    }

    private static async Task<int> TestAsync()
    {
        Console.WriteLine("Before await");
        await Task.Run(() => { Thread.Sleep(2000); return; });
        Console.WriteLine("After await");
        return 2;
    }
}

"@

Add-Type -TypeDefinition $source
[TestClass]::TestWrapper()
Run Code Online (Sandbox Code Playgroud)

在这里,我只是调用异步函数,等待它完成,并返回结果.在函数内部,我只是在返回一个值之前和之后尝试在线程休眠之前和之后进行记录,以便我知道该函数已完全执行.

我希望看到:

Before await
After await
2
Run Code Online (Sandbox Code Playgroud)

这就是我作为C#控制台应用程序或基本PowerShell运行时所看到的,但是当通过PowerShell ISE运行时,我看到:

Before await
2
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么会这样吗?

.net c# powershell asynchronous

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

如何在powershell中创建毫秒级的时间跨度?

New-Timespan不带"MilliSeconds"参数,如何从毫秒创建TimeSpan?

powershell

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

Powershell表示使用get-filehash时找不到路径

首先,我的代码:

gci -Path C:\ -Recurse | select FullName | %{Get-FileHash $_}

gci -Path C:\ -Recurse | select FullName输出错误,但输出正确的路径.

我在这做错了什么?

powershell

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

如何从powershell打开excel工作簿进行自动化

我想打开一个excel工作簿并读出数据,做其他类型的操作等.我知道我必须添加一个程序集引用:

 [Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft Office\Office16\ADDINS\Microsoft Power Query for Excel Integrated\bin\Microsoft.Office.Interop.Excel.dll")
Run Code Online (Sandbox Code Playgroud)

然后我需要实例化一个Application对象.

$workbook = New-Object -TypeName Microsoft.Office.Interop.Excel.Application
Run Code Online (Sandbox Code Playgroud)

然而,这会返回错误"未找到构造函数".实际上,Microsoft.Office.Interop.Excel.Application的接口方式不是这样吗?我想知道如何在这种情况下实例化它.

.net excel powershell automation

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