我一直在寻找一个简短的代码来执行此操作一段时间,最后自己弄清楚了。我想我会分享它。
$FQDN = "madeup.name.domain.co.in"
Run Code Online (Sandbox Code Playgroud)
我需要的结果是name.domain.co.in
我正在尝试运行 PowerShell 脚本以owncloud-client从 Windows 10 虚拟机中卸载。下面显示的代码可以在以下位置找到
https://chocolatey.org/packages/owncloud-client (tools\chocolateyUninstall.ps1)
我只想通过从 PowerShell 运行代码来测试代码,看看它是否确实卸载了软件。
$unfile = "ownCloud\uninstall.exe"
if (Test-Path "${Env:ProgramFiles(x86)}\$unfile") {
$unpath = "${Env:ProgramFiles(x86)}\$unfile"
} else {
$unpath = "${Env:ProgramFiles}\$unfile"
}
Uninstall-ChocolateyPackage 'owncloud-client' 'exe' '/S' "$unpath"
Run Code Online (Sandbox Code Playgroud)
我通过键入以下内容从包含该脚本的目录运行脚本:
.\chocolateyUninstall.ps1
Run Code Online (Sandbox Code Playgroud)
作为输出,我收到以下错误:
Uninstall-ChocolateyPackage is not recognized as the name of cmdlet, function, script file, or operable program.
Run Code Online (Sandbox Code Playgroud)
Uninstall-ChocolateyPacakge.ps1 软件包由 Chocolatey 提供。我检查了我的机器,这个包位于:
C:\ProgramData\chocolatey\helpers\functions\Uninstall-ChocolateyPackage.ps1
Run Code Online (Sandbox Code Playgroud)
但它仍然不被 powershell 识别为 cmdlet。我怎么解决这个问题?谢谢
我将 Windows 用户帐户凭据作为参数传入 CloudFormation 模板中。使用 SSM/EC2Config,我需要在与此模板关联的实例上执行命令,但由于 Windows 上只有一个特定用户帐户被授予对我所需资源的访问权限,因此当我通过以下方式执行 Powershell 命令时,我需要指定这些相同的凭据SSM(因为仅以管理员身份运行将没有适当的访问权限)。
这些命令将在稍后运行,而不是在实例启动时运行。我有什么办法可以从 CloudFormation 获取这些凭据吗?或者还有其他方法可以实现此目的或类似的目的吗?
这对我来说工作:
Get-WinEvent -FilterHashTable @{Logname = "ForwardedEvents" ; ID = 4625,4740}
Run Code Online (Sandbox Code Playgroud)
(……我期望的结果……)
这有效:
$EventId = "4625"
Get-WinEvent -FilterHashTable @{Logname = "ForwardedEvents" ; ID = $EventId}
Run Code Online (Sandbox Code Playgroud)
这不起作用:
$EventId = "4625,4740"
Get-WinEvent -FilterHashTable @{Logname = "ForwardedEvents" ; ID = $EventId}
Run Code Online (Sandbox Code Playgroud)
错误...
Get-WinEvent : No events were found that match the specified selection criteria.
At line:1 char:13
+ Get-WinEvent <<<< -FilterHashTable @{Logname = "ForwardedEvents" ; ID = $EventIds}
+ CategoryInfo : ObjectNotFound: (:) [Get-WinEvent], Exception
+ FullyQualifiedErrorId : NoMatchingEventsFound,Microsoft.PowerShell.Commands.GetWinEventCommand
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
我尝试了下面的命令
Get-Module -ListAvailable
Run Code Online (Sandbox Code Playgroud)
但它没有在列表中向我显示“故障转移集群”
从哪里可以下载这个模块?我需要安装任何 SDK 吗?注意:我不想安装 Azure SDK。还有其他办法吗?
更新 我正在使用本地 Windows 10 远程连接到 Azure 虚拟机 (Windows Server 2012 R2)。我确实想使用本地 Windows 10 计算机上的 powershell 管理所有配置。
我想从 powershell 内的新窗口/独立实例中启动 bat 文件。
bat 文件将从 powershell 获取计算机名,
例如
mybat.bat $thiscomputer
Run Code Online (Sandbox Code Playgroud)
我试过了
start mybat.bat $thiscomputer
start "cmd /c" mybat.bat $thiscomputer
start /k mybat.bat $thiscomputer
start-process mybat.bat $thiscomputer
Run Code Online (Sandbox Code Playgroud)
通常,bat 确实会运行,但保留在 powershell 脚本内。
我需要 powershell 脚本在新窗口中启动 bat 并循环回到开头。
谢谢
孔夫塞斯
我有这个:
Get-ChildItem c:\H\admin\contents
Run Code Online (Sandbox Code Playgroud)
是否可以做同样的事情,但相对于 powershell 脚本运行的文件夹。有时我在 c\H\admin 中运行脚本,有时在 c\H\user 目录中运行,所以我宁愿不必更改我的脚本中的所有路径。
我正在参加针对初学者的 PowerShell 课程,而讲师在寻求帮助时完全没用。我想要执行的任务是:在我的计算机中搜索以 .doc、.docx、.xls 或 .xlsx 文件扩展名结尾的文件。\n将文件名和大小(按文件扩展名分组)输出到名为\xe2\x80\x9cFile_Summary.txt\xe2\x80\x9d。输出还应包含每个文件扩展名的文件总数和总文件大小。到目前为止我创建的代码是:
\n\n$path = ".\\"\n$destination = "C:\\Users\\StayPositibve\\Desktop\\pleasework1.txt"\n\n$results = Get-ChildItem .\\* -Recurse | where {$_.extension -in ".doc",".docx",".xls",".xlsx"} | Select-Object Name, Extension, @{Name="Kbytes";Expression={ "{0:N0}" -f ($_.Length / 1Kb) }}\n$results | Sort-Object -Property extension | Out-File $destination\n$countFiles = Get-ChildItem .\\* -Recurse | where {$_.extension -in ".doc",".docx",".xls",".xlsx"} \n$countFiles | Group-Object -Property extension | Out-File $destination -Append\nRun Code Online (Sandbox Code Playgroud)\n\n代码运行(尽管我确信这是糟糕的编码);但是,我不知道如何将每个扩展名的总文件大小加在一起。我有每个扩展名的文件总数,但没有总大小。感谢任何人都可以给我的帮助!
\n我已经知道如何在 powershell 中创建一个函数。问题是:如何保存该函数以便将来使用它?
当我在 Powershell 中编写 myFunction{3+3} 时,我可以在该会话中使用该函数。
但是,如果我退出 powershell 并再次打开它,该功能就消失了。如何“保存”该函数,以便在重新启动 Powershell 后也可以使用它?
$size = get-psdrive c
echo $size.used
Run Code Online (Sandbox Code Playgroud)
如何获取 C 盘已用空间的值(MB 和 GB)?我环顾四周,找不到正确的答案。请帮忙。谢谢!!!
powershell ×10
batch-file ×1
chocolatey ×1
cmd ×1
event-log ×1
file ×1
function ×1
save ×1
size ×1
windows ×1