标签: powershell

读取 Registery 值并转换为日期

我正在尝试使用以下代码查找 Windows 机器的正常运行时间(上次关机时间):

$computernames = gc LegAservers.txt
foreach ($computername in $computernames) {
  $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$computername)
  $sKey = "System\CurrentControlSet\Control\Windows"
  $key = $baseKey.OpenSubKey($sKey)
  $kvalue = $key.GetValue("ShutdownTime")
  $kvalue
  "------------------------------------------"
  $baseKey.Close()
}
Run Code Online (Sandbox Code Playgroud)

输出是:

$computernames = gc LegAservers.txt
foreach ($computername in $computernames) {
  $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$computername)
  $sKey = "System\CurrentControlSet\Control\Windows"
  $key = $baseKey.OpenSubKey($sKey)
  $kvalue = $key.GetValue("ShutdownTime")
  $kvalue
  "------------------------------------------"
  $baseKey.Close()
}
Run Code Online (Sandbox Code Playgroud)

我无法隐蔽这种$kvalue在DateTime使用格式为:

[DateTime]::FromFileTime($kvalue)
Run Code Online (Sandbox Code Playgroud)

请建议正确的$kvalue日期和时间格式转换方法,以便用户理解。

registry powershell datetime

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

添加输出流时,布尔函数会更改返回的数据类型

我在理解 powershell 的内部工作原理时遇到了一些麻烦。以这个例子为例。我有一个布尔函数 (Test-MyCode),只要省略了 Write-Output cmdlet,它就可以正常工作。添加 Write-Output cmdlet 后,返回类型将更改为 Object[] 数组。

.GetType() 可用于查看数据类型。

为什么是这样?

function Test-MyCode
{
    if( 2 -gt 0)
    {
        Write-Output "This function will return false"
        return $false
    }
    else
    {
        Write-Output "could return true if condition is changed"
        return $true
    }
}

function Invoke-MyCode
{
    Write-Output "this is main"
    Write-Output "do stuff"
    # Test-MyCode is configured to return false... yet its true
    if (Test-MyCode)
    {
        Write-Output "yep, no longer boolean"
    }

}

Invoke-MyCode
Run Code Online (Sandbox Code Playgroud)

powershell

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

使用 Powershell 获取远程服务器上的文件夹大小

我正在尝试获取远程计算机路径上的文件夹结构和每个文件夹大小。我能够获取文件夹结构,但无论我指定的任何文件夹路径如何,文件夹大小都会返回 901.00 KB 的标准数字。我可以看到脚本能够正确获取远程文件夹路径,但不能正确获取文件/文件夹大小。

这是我到现在为止想出的代码。

param(
    [Parameter(ValueFromPipeline=$True, Mandatory=$True)]
    [System.String] $ComputerName,
    [Parameter(ValueFromPipeline=$True, Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
    [ValidateNotNullOrEmpty()]
    [System.String] $Path
 )

function Get-FolderSize ($_ = (get-item .))  {
Process {
$RemoteServer = @($ComputerName)
ForEach($Computer in $ComputerName) {
$ErrorActionPreference = "SilentlyContinue"
$length = Invoke-Command -ComputerName $ComputerName { (Get-ChildItem $_.fullname -recurse | Measure-Object -property length -sum).sum 
} -ArgumentList $Path
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Folder ($_.FullName)
$obj | Add-Member NoteProperty Length ($length)
Write-Output $obj

  }
 }
}

Function Class-Size($size)
{
 IF($size -ge 1GB) …
Run Code Online (Sandbox Code Playgroud)

powershell

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

获取出站 IP 范围 azure 网站 PowerShell

是否有任何 powershell 命令可以获取 azure 网站的所有出站 IP 地址?

powershell azure web azure-web-app-service

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

-in 运算符在 2.0 版中不起作用?

我有以下代码行在 powershell 3.0 中工作

$Type = "DD"
if ($Type -in "AA","BB","CC") {Write-Host  "Type = $Type" -ForegroundColor Yellow }
Run Code Online (Sandbox Code Playgroud)

但是,使用 PowerShell 2.0 运行时出现错误

- : You must provide a value expression on the right-hand side of the '-' operator.
    + CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression
Run Code Online (Sandbox Code Playgroud)

为什么呢?我该如何解决?

powershell powershell-2.0

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

PowerShell:try-catch 不起作用

我有一个 PowerShell 脚本,它从文件中获取文件名列表,在文件夹中搜索文件名,将它们存档,然后执行其他操作。

#make non-terminating errors behave like terminating errors (at script level)
$ErrorActionPreference = "Stop"

#set the folder that has the list and the files
$some_path = "D:\some_folder\"
$archive = "D:\archive\"

#set the list file name
$file_list = $some_path + "file_list.txt"

#get the files that I'm searching for from this list file
$files_to_retrieve = Select String -Path $file_list -Pattern "something" | Select-Object Line

#get the number of files for this search string
$n = $file_list.Length - 1

#seed the …
Run Code Online (Sandbox Code Playgroud)

powershell

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

如何将数组的输出存储在文件中?

我有一个小的 powershell 脚本,可以远程从服务器收集指标。通常调用函数来收集 CPU / 内存 / HDD 信息。

我从文本文件中提取服务器,并为每台机器调用函数。

foreach ($i in Get-Content "C:\Servers.txt")
{{"=========================="; $i + "`n" + "==========================";
 "Number of CPU Cores" ; CPU-Info ; "Memory in GB" ; Memory-Info
  ; "HDDs in GB" ; HDD-Size } 
Run Code Online (Sandbox Code Playgroud)

首先,我尝试使用 outfile 所有内容,out-file但当然每个新输入都会覆盖我的文件。有没有办法临时存储输出并以一种好的方式输出它?

你会推荐什么?也许有更顺畅的方式来做到这一点?

powershell scripting powershell-2.0 powershell-3.0 powershell-4.0

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

2 行正则表达式

我正在尝试在文本文件中搜索 2 行文本(一个总是一个接一个地放置)。一行总是相同的:#EXTINF:Discovery Science. 然而,第二个总是不同的,但具有以下模式:“http://”ipaddress“:”一些随机端口ip地址“:”随机端口

例子:

#EXTINF:Discovery Science http://95.188.124.44:1234/udp/233.7.70.27:5000
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

$file = "C:\1.m3u"
$Text = [IO.File]::ReadAllText($file) 
[regex]$reg = "([#EXTINF:Discovery Science http://\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b])"
$match = $reg.match($Text)  
$array = @()
$array = new-object collections.arraylist
$array = while ($match.Success)  
{            
    $match.Groups[1].value            
    $match = $match.nextMatch()
    write-host $match
}
Run Code Online (Sandbox Code Playgroud)

regex powershell

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

将 Windows 路径更改为 unix 路径

我有一个脚本,它定义了一个值为“X:\data\tables\”的变量 $winpath。此路径值不能在另一个函数使用时更改,但是是否可以将此值转换/转换为 unix 方式(使用某些分隔符或任何其他方法?)并存储在另一个变量中?即 /x/data/tables

$server = 10.1.1.1
$user = jason
$winpath = X:\data\tables\
Write-Output $user + '@' + $server + ':' + $winpath
Run Code Online (Sandbox Code Playgroud)

期望输出-

jason@10.1.1.1/x/data/tables
Run Code Online (Sandbox Code Playgroud)

powershell

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

命令行禁用netbios?

我有一个以太网适配器和一个无线适配器,但终生无法弄清楚用于禁用系统上所有适配器的 TCP/IP 上的 Netbios 的命令行(或 powershell)。我将不胜感激。

在此处输入图片说明

powershell netbios command-line-interface

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