标签: powershell

大文件复制期间的进度(Copy-Item&Write-Progress?)

有没有办法在PowerShell中复制一个非常大的文件(从一个服务器到另一个服务器)并显示其进度?

有一些解决方案可以将Write-Progress与循环结合使用来复制许多文件并显示进度.但是,我似乎无法找到任何可以显示单个文件进度的内容.

有什么想法吗?

powershell progress copy-item copying progress-bar

56
推荐指数
6
解决办法
10万
查看次数

枚举类似于PowerShell中的switch参数

我以这种方式在PowerShell脚本中使用switch参数.

param(
    [switch] $Word,
    [switch] $Excel,
    [switch] $powerpoint,
    [switch] $v2007,
    [switch] $v2010,
    [switch] $x86,
    [switch] $x64,
)
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚任何整洁的方式让它更多的枚举风格.正如任何人可能猜到的那样,我希望用户在word,excel和powerpoint之间进行选择.在x2007和v2010之间.

有没有一个简洁的方法来获得输入参数枚举风格?

我是PowerShell的新手.所以,如果这听起来像我不知道一些明显的东西,那么请指出我可以阅读的一些链接.

powershell

56
推荐指数
4
解决办法
4万
查看次数

检查Windows PowerShell中是否存在文件?

我有这个脚本,它比较磁盘的两个区域中的文件,并将最新的文件复制到具有较旧修改日期的文件上.

$filestowatch=get-content C:\H\files-to-watch.txt

$adminFiles=dir C:\H\admin\admin -recurse | ? { $fn=$_.FullName; ($filestowatch | % {$fn.contains($_)}) -contains $True}

$userFiles=dir C:\H\user\user -recurse | ? { $fn=$_.FullName; ($filestowatch | % {$fn.contains($_)}) -contains $True}

foreach($userfile in $userFiles)
{

      $exactadminfile= $adminfiles | ? {$_.Name -eq $userfile.Name} |Select -First 1
      $filetext1=[System.IO.File]::ReadAllText($exactadminfile.FullName)
      $filetext2=[System.IO.File]::ReadAllText($userfile.FullName)
      $equal = $filetext1 -ceq $filetext2 # case sensitive comparison

      if ($equal) { 
        Write-Host "Checking == : " $userfile.FullName 
        continue; 
      } 

      if($exactadminfile.LastWriteTime -gt $userfile.LastWriteTime)
      {
         Write-Host "Checking != : " $userfile.FullName " >> user"
         Copy-Item -Path …
Run Code Online (Sandbox Code Playgroud)

powershell powershell-3.0

56
推荐指数
5
解决办法
21万
查看次数

如何在PowerShell中将环境变量打印到控制台?

我开始使用PowerShell,并试图找出如何echo通过控制台的系统环境变量来读取它.

以下两者都不起作用.第一个只打印%PATH%,第二个打印没有.

echo %PATH%
echo $PATH
Run Code Online (Sandbox Code Playgroud)

powershell

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

为什么我的本地创建的脚本不允许在RemoteSigned执行策略下运行?

我在64位Windows 7 Professional上使用Windows PowerShell 2.0 .我的桌面上有一个脚本,当我尝试运行它时会导致以下错误:

File C:\Users\UserName\Desktop\Script.ps1 cannot be loaded. The file C:\Users\UserName\Desktop\Script.ps1 is not digitally signed. The script will not execute on the system.  Please see "get-help about_signing" for more details..
At line:1 char:54
+ C:\Users\UserName\Desktop\TestGetWindowsUpdateLog.ps1 <<<<
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException
Run Code Online (Sandbox Code Playgroud)

我既是域管理员又是本地管理员,如果我运行Get-ExecutionPolicy -List,我可以看到Group Policy Object我创建的配置PowerShell是RemoteSigned在机器级正确应用执行策略:

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy    RemoteSigned
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine       Undefined
Run Code Online (Sandbox Code Playgroud)

我自己在记事本中 …

powershell code-signing powershell-2.0

55
推荐指数
6
解决办法
14万
查看次数

$?之间的区别?和PowerShell中的$ LastExitCode

在PowerShell中,$?和之间有什么区别$LastExitCode?

我读到了自动变量,它说:

$? Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.

$LastExitCode Contains the exit code of the last Windows-based program that was run.

在$?它的定义中并没有解释成功和失败意味着什么.


我问,因为$?当且仅当$ LastExitCode为0时,我才认为这是真的,但我发现了一个令人惊讶的反例:$ LastExitCode = 0但是PowerShell中的$?= False.将stderr重定向到stdout会产生NativeCommandError.

windows powershell command-line

55
推荐指数
1
解决办法
6万
查看次数

在PowerShell中包含相关文件

我想包含具有这种伪语法的脚本文件:

Include '.\scripA.ps1'
Run Code Online (Sandbox Code Playgroud)

但我发现的唯一的事情就是这样:

$thisScript = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
. ($thisScript + '.\scriptA.ps1')
Run Code Online (Sandbox Code Playgroud)

那很难看.

是否有一些很好的方法来包含具有相对路径的脚本?

powershell relative-path include

54
推荐指数
5
解决办法
6万
查看次数

PowerShell:创建本地用户帐户

我需要创建一个新的本地用户帐户,然后将它们添加到本地Administrators组.可以在PowerShell中完成吗?

编辑:

# Create new local Admin user for script purposes
$Computer = [ADSI]"WinNT://$Env:COMPUTERNAME,Computer"

$LocalAdmin = $Computer.Create("User", "LocalAdmin")
$LocalAdmin.SetPassword("Password01")
$LocalAdmin.SetInfo()
$LocalAdmin.FullName = "Local Admin by Powershell"
$LocalAdmin.SetInfo()
$LocalAdmin.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD
$LocalAdmin.SetInfo()
Run Code Online (Sandbox Code Playgroud)

我有这个,但想知道是否还有更多的PowerShell风格.

powershell powershell-2.0

54
推荐指数
4
解决办法
17万
查看次数

PowerShell的Invoke-RestMethod相当于curl -u(基本身份验证)

相当于什么

curl -u username:password ...
Run Code Online (Sandbox Code Playgroud)

在PowerShell中Invoke-RestMethod?我试过这个:

$securePwd = ConvertTo-SecureString "password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePwd)

Invoke-RestMethod -Credential $credential ...
Run Code Online (Sandbox Code Playgroud)

但它返回401,未经授权.

powershell curl basic-authentication

54
推荐指数
7
解决办法
6万
查看次数

术语"ng"未被识别为cmdlet的名称

今天,在完成一些基本的AngularJS简介时,我遇到了一个问题.我打开PowerShell来开始这个项目.NPM工作.

我能够使用安装Angular

npm install -g @angular/cli
Run Code Online (Sandbox Code Playgroud)

任何时候我试图运行我会得到

the term 'ng' is not recognized as the name of a cmdlet
Run Code Online (Sandbox Code Playgroud)

powershell node.js npm angular-cli angular

54
推荐指数
12
解决办法
9万
查看次数