标签: powershell

比较Powershell中的System.Version

我有一种情况,我必须根据比较版本在脚本中做出决定。请考虑以下示例:

PS C:\>
[version]$SomeVersion='1.1.1'
[version]$OtherVersion='1.1.1.0'

PS C:\> $SomeVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
1      1      1      -1      

PS C:\> $OtherVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
1      1      1      0       

PS C:\>$SomeVersion -ge $OtherVersion
False
Run Code Online (Sandbox Code Playgroud)

比较类型为System.Version的对象时,我想省略修订版,但
我找不到任何明智的方法。
有没有?

注意-我已经尝试做过:

PS C:\> ($scriptversion |select major,minor,build) -gt ($currentVersion|select major,minor,build)

Cannot compare "@{Major=1; Minor=1; Build=1}" to "@{Major=1; Minor=1; 
Build=1}" because the objects are not the same type or the object "@{Major=1; 
Minor=1; Build=1}" does not implement "IComparable".
At …
Run Code Online (Sandbox Code Playgroud)

powershell

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

错误重定向在PowerShell中不起作用

当我在powershell中输入'4abd'时,它给出了以下输出.

4abd:术语"4abd"未被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.在行:1 char:1 + 4abd + ~~~~ + CategoryInfo:ObjectNotFound:(4abd:String)[],CommandNotFoundException + FullyQualifiedErrorId:CommandNotFoundException

我想将此错误重定向到文件.我尝试了以下所有方法:

4abd 2>&1>>"D:\\xyz.txt"
4abd 2>&1 >> D:\\xyz.txt
4abd 2>&1 >> xyz.txt
4abd 2>&1 >> "xyz.txt"
4abd 2>"D:\\xyz.txt"
4abd 2>D:\\xyz.txt
Run Code Online (Sandbox Code Playgroud)

但没有任何结果.请帮我解决这个问题.

powershell redirect

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

将字符串转换为PowerShell DateTime

我有以下字符串:"02-06-2018 16:25:28"。我需要将其转换为DateTime对象。我尝试这样做:

[DateTime]::ParseExact('02-06-2018 16:25:28', 'dd-MM-yyyy hh:mm:ss', $null)
Run Code Online (Sandbox Code Playgroud)

但这没有用:

无法将字符串识别为有效的DateTime。

除了[DateTime]::ParseExact支持在此fomat中解析字符串之外,还有其他功能dd-MM-yyyy hh:mm:ss吗?

我正在使用PowerShell v5.1。

powershell powershell-5.0

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

如何以加速的方式获取Powershell类型的加速器列表?

根据该Technet关于Powershell类型加速器的文章,有几十种类型别名称为类型加速器。确实,以下命令

[psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::get
Run Code Online (Sandbox Code Playgroud)

在我的系统上返回80个加速器。

但是,它的简写[accelerators]::get似乎失败了:

找不到类型[加速器]。确保包含此类型的程序集已加载。在第1行:char:1 + [accelerators] :: get + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(accelerators:TypeName)[],RuntimeException + FullyQualifiedErrorId :TypeNotFound

我还尝试在发出命令之前动态加载System.Management.Automation.TypeAccelerators程序集[System.Reflection.Assembly]::LoadWithPartialName("System.Management.Automation.TypeAccelerators"),但是仍然失败。

$PSVersionTable 返回以下数据:

Name                           Value                                                                                                                                                          
----                           -----                                                                                                                                                          
PSVersion                      4.0                                                                                                                                                            
WSManStackVersion              3.0                                                                                                                                                            
SerializationVersion           1.1.0.1                                                                                                                                                        
CLRVersion                     4.0.30319.42000                                                                                                                                                
BuildVersion                   6.3.9600.18728                                                                                                                                                 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                                                                                                                           
PSRemotingProtocolVersion      2.2            
Run Code Online (Sandbox Code Playgroud)

操作系统是Windows 7 x64。

问题:如何以加速方式获取Powershell类型的加速器列表?

powershell powershell-4.0 type-accelerators

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

VSTS docker任务在docker RUN命令中的'&&'令牌上失败

我在VSTS(在线Visual Studio)中为ASP.NET Core应用程序配置CI。我添加了“ docker-compose build”任务来构建定义,但是它失败并出现错误:

    步骤4/9:运行dotnet还原QuizService.sln && dotnet发布QuizService.sln -c版本-o obj / Docker / publish
    --->在7ea0cf1881d1中运行
    ... rence ='SilentlyContinue'; dotnet恢复QuizService.sln && dotnet ...
    在此版本中,标记“ &&”不是有效的语句分隔符。
    + CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException
    + FullyQualifiedErrorId:InvalidEndOfLine
    服务'quizservice'构建失败:命令'powershell -Command $ ErrorActionPreference ='Stop'; $ ProgressPreference ='SilentlyContinue'; dotnet恢复QuizService.sln && dotnet发布QuizService.sln -c版本-o obj / Docker / publish'返回非零代码:1
    ## [错误]构建测验服务
    ## [error] Service'quizservice'生成失败:命令'powershell -Command $ ErrorActionPreference ='Stop'; $ ProgressPreference ='SilentlyContinue'; dotnet恢复QuizService.sln && dotnet发布QuizService.sln -c版本-o obj / Docker / publish'返回非零代码:1
    ## [错误] C:\ ProgramData \ Chocolatey \ bin \ docker-compose.exe失败,返回码:1

问题出在我的dockerfile上:

运行dotnet还原QuizService.sln && …

powershell docker azure-devops asp.net-core

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

使用PowerShell设置新的smb共享时出错

我正在编写一个PowerShell脚本,它将在c驱动器上设置文件夹结构,然后将这些文件夹转换为共享.

使用New-SmbSharecmdlet时,我收到1332或50错误.

使用域我得到1332错误

New-SmbShare -Name "InstallerFiles" -Path "C:\SoftwareDistribution\InstallerFiles" -ContinuouslyAvailable $true -ReadAccess "domain\Authenticated Users"

New-SmbShare : No mapping between account names and security IDs was done. 
At line:1 char:1
+ New-SmbShare -Name "InstallerFiles" -Path "C:\SoftwareDistribution\InstallerFile ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MSFT_SMBShare:ROOT/Microsoft/Windows/SMB/MSFT_SMBShare) [New-SmbShare], CimException
    + FullyQualifiedErrorId : Windows System Error 1332,New-SmbShare
Run Code Online (Sandbox Code Playgroud)

如果我删除域名,我会得到50错误.

New-SmbShare -Name "InstallerFiles" -Path "C:\SoftwareDistribution\InstallerFiles" -ContinuouslyAvailable $true -ReadAccess "Authenticated Users"

New-SmbShare : The request is not supported. 
At line:1 char:1
+ New-SmbShare -Name "InstallerFiles" -Path "C:\SoftwareDistribution\InstallerFile …
Run Code Online (Sandbox Code Playgroud)

windows powershell smb

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

Powershell Set-MpPreference -DisableRealtimeMonitoring $ true无法正常工作

我必须警告您,我不太会使用Powershell。我正在尝试通过Powershell关闭Windows Defender实时保护,我找到了该命令Set-MpPreference -DisableRealtimeMonitoring $true并以管理员权限对其进行了尝试,只是为了获得此权限

Set-MpPreference:操作失败,出现以下错误:0x800106ba。操作:Set-MpPreference。目标:DisableRealtimeMonitoring。在第1行上:char:1
+ Set-MpPreference -DisableRealtimeMonitoring $ true
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified:(MSFT_MpPreference:ROOT \微软... FT_MpPreference)
[设置MpPreference] CimException
+ FullyQualifiedErrorId:HRESULT 0x800106ba,SET-MpPreference

有什么想法吗?

security powershell cmd windows-defender

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

Azure自动化Powersell Runbook失败:'Invoke-Sqlcmd'无法识别为cmdlet的名称

我已完成以下教程的所有步骤,以获得用于自动化索引和统计维护的Runbook:

https://blogs.msdn.microsoft.com/azuresqldbsupport/2018/01/15/automating-azure-sql-db-index-and-statistic-maintenance-using-azure-automation/

我能够毫无错误地完成所有教程,但是当我执行Runbook时,它返回一个错误:

Invoke-Sqlcmd : The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:6 char:16
+ $SQLOutput = $(Invoke-Sqlcmd -ServerInstance $AzureSQLServerName -Use ...
+ ~~~~~~~~~~~~~
+ CategoryInfo: ObjectNotFound: (Invoke-Sqlcmd:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

我能做什么?我在Azure中使用SQL Server.具体而言,Azure SQL数据库具有定价/模型层"S4标准(200 DTU)".

powershell maintenance automation azure runbook

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

Azure App Service ApiApp

我正在尝试使用PowerShell 在Azure App Service中创建API应用程序.

我调用的cmdlet默认情况下始终创建Web应用程序.如果有可能,我想知道如何指定类型/种类Api App而不是Web App?

New-AzureRmWebApp -Name $name -Location $location -AppServicePlan $plan -ResourceGroupName $resourceGroup
Run Code Online (Sandbox Code Playgroud)

从我的阅读中,除了图标之外,两者之间没有太大的不同,是否值得将类型设置为"Api App",如果它是我的应用程序的全部内容?

我使用的是AzureRM PowerShell模块的5.4.0版.

> Get-Module "AzureRM"

ModuleType Version    Name                                                 
---------- -------    ----
Script     5.4.0      AzureRM
Run Code Online (Sandbox Code Playgroud)

powershell azure azure-web-sites azure-api-apps azure-web-app-service

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

正则表达式翻转并合并除第一个之外的所有反斜杠

我正在尝试编写正则表达式,它将翻转和合并除第一个之外的所有反斜杠.

像这样的东西:

 C:\a\b\c\d\e         --> C:/a/b/c/d/e
 C:\a\\b\\\c\d\\\\\\e --> C:/a/b/c/d/e
 C:/a///b//c/d//////e --> C:/a/b/c/d/e
 C:\a/\/b/\c/d//\///e --> C:/a/b/c/d/e
 C:/a/b/c/d/e         --> C:/a/b/c/d/e
Run Code Online (Sandbox Code Playgroud)

但

\\my_share\a\b\c\d/e             --> //my_share/a/b/c/d/e
\\my_share\\\a\\\\b\c\\\//\\d\e   --> //my_share/a/b/c/d/e
\\/\my_share\\\a\\\\b\c\\\\\\d\e --> //my_share/a/b/c/d/e (if multiple '\' or\and '/' in the front - put two //)
\my_share\\\a\\\\b\c\\\\\\d\e    --> /my_share/a/b/c/d/e (if one '\' or\and '/' in the front - flip it)
my_share\\\a\\\\b\c\\\\\\d\e     --> my_share/a/b/c/d/e (if no '\' or\and '/' in the front - don't do anything)
Run Code Online (Sandbox Code Playgroud)

如何在powershell中做到这一点?$my_path -ireplace "\\", "/"?

regex powershell

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