AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-154815

mahmood's questions

Martin Hope
mahmood
Asked: 2021-02-04 12:12:59 +0800 CST

通过powershell远程IP更改后断开连接

  • 5

使用以下 powershell 代码,我可以修改远程 Windows 7 机器的静态 IP。

Write-Host("Modifying IP... ")
Invoke-Command -ComputerName $RemoteMachine -Credential $Cred -ScriptBlock { 
           & {
           $adapter = Get-WmiObject win32_NetworkAdapterConfiguration -filter "Index = 7"
           $adapter.EnableStatic($Using:NewIP,"255.255.255.0")
           } } 

这是我脚本的最后一部分。更改IP后,连接丢失。这是意料之中的。问题是脚本仍然尝试重新连接大约 4 分钟,然后它放弃了。输出如下所示:

Modifying IP... 
WARNING: The network connection to 10.1.1.50 has been interrupted. Attempting to reconnect for up to 4 minutes...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: Attempting to reconnect to 10.1.1.50 ...
WARNING: The reconnection attempt to 10.1.1.50 failed. Attempting to disconnect the session...
WARNING: Reconnection attempt canceled. Please repair the network connection and reconnect using Connect-PSSession or Receive-PSSession.

PS C:\Windows\system32>

我怎样才能避免这种情况?

powershell
  • 2 个回答
  • 331 Views
Martin Hope
mahmood
Asked: 2021-02-03 11:49:35 +0800 CST

在 Powershell 中使用 Format-Table 对表进行排序

  • 6

使用以下代码,我可以获取 VM 名称及其 IP 地址。我想知道是否可以根据 IP 列对行进行排序(升序)?我希望看到 IPv4 的排序列表。

Get-VM | Get-VMGuest | Format-Table VM, IPAddress

输出是

VM                           IPAddress                             
--                           ---------                             
N1                           {10.1.1.32, fe80::51b:9698:4853:2ee5} 
N2                           {10.1.1.18, fe80::b640:8c2a:837c:613e}
powershell
  • 1 个回答
  • 4793 Views
Martin Hope
mahmood
Asked: 2021-02-02 07:06:20 +0800 CST

为计划的 powershell 脚本指定日志文件

  • 6

我在 Windows 中创建了一个计划任务以在特定时间运行 powershell 脚本。如您所见,在操作页面中,我已将 powershell 路径指定为程序,并将我的脚本指定为参数。计划任务工作正常。

在此处输入图像描述

我想包括我自己的日志文件,在那里打印我的脚本的输出。我怎样才能做到这一点?

powershell scheduled-tasks
  • 2 个回答
  • 2477 Views
Martin Hope
mahmood
Asked: 2021-02-02 05:47:35 +0800 CST

在powershell中标记字符串数组

  • 5

我使用此代码从 vSphere 检索虚拟机列表。

[string]$arrayVM = (Get-VM | Select-Object -Property Name).Name
Write-Host($arrayVM)

很好,因为我得到以下输出

N1.windows1 N2.linux1 N3.linux2

但是,然后我使用Write-Host($arrayVM[0]),我得到N。我想得到N1.windows1。我该如何解决?

powershell powercli
  • 1 个回答
  • 37 Views
Martin Hope
mahmood
Asked: 2021-01-09 07:27:02 +0800 CST

在PowerShell中编写应用程序的完整路径

  • 5

我想在这样的远程机器上运行命令

Invoke-Command -ComputerName $RemoteMachine -Credential $Cred `
    -ScriptBlock { echo MYPASS | "C:\Program Files (x86)\AnyDesk\AnyDesk.exe" --set-password}

但是,我收到此错误

At C:\Users\user\Desktop\anydesk.ps1:14 char:33
+ ... o MYPASS | "C:\Program Files (x86)\AnyDesk\AnyDesk.exe" --set-pass ...
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.

使用管道时,指定程序的路径似乎有问题。我该如何解决?

更新:

由于我想在具有多个命令的脚本块中使用它并将变量传递给脚本块,因此以下修改也不起作用。

$NewPassword = 'Mahmood00'
Invoke-Command -ComputerName $RemoteMachine -Credential $Cred -ScriptBlock {        
         & {
         Param(param1)
 'echo $param1 | "C:\Program Files (x86)\AnyDesk\AnyDesk.exe" --set-password'
          } } ArgumentList = $Using:NewPassword

那有语法问题。如果我使用

$NewPassword = 'Mahmood00'
Invoke-Command -ComputerName $RemoteMachine -Credential $Cred -ScriptBlock {        
         & Param(param1){
 'echo $param1 | "C:\Program Files (x86)\AnyDesk\AnyDesk.exe" --set-password'
          } } ArgumentList = $Using:NewPassword
              

我收到此错误:

A Using variable cannot be retrieved. A Using variable can be used only with Invoke-Command, 

更新 2:

使用局部变量也不起作用。见下文:

Invoke-Command -ComputerName $RemoteMachine -Credential $Cred -ScriptBlock {        
         &   {
         $param1 = $Using:NewPassword
         cmd /c 'echo $param1 | "C:\Program Files (x86)\AnyDesk\AnyDesk.exe" --set-password'
          } } 

密码实际上是 $param1 而不是该变量的值。

powershell
  • 2 个回答
  • 610 Views
Martin Hope
mahmood
Asked: 2021-01-08 05:04:28 +0800 CST

使用管理员帐户调用命令并运行命令

  • 8

使用以下命令,我试图dir c:从 powershell 使用管理员帐户运行

$RemoteMachine = "10.1.1.40"
$Username = 'Administrator'
$Password = ' '
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName $RemoteMachine -Credential $Cred -ScriptBlock { dir c: }

但是,输出是:

PS C:\Windows\system32> C:\Users\user\Desktop\anydesk.ps1

PS C:\Windows\system32>

这意味着,它还没有连接到远程机器。我不得不说启用了管理员帐户并且远程桌面允许管理员。有什么办法解决这个问题吗?如果我使用普通用户,则能够看到dir c:.

在此处输入图像描述

在此处输入图像描述

powershell windows-7
  • 1 个回答
  • 676 Views
Martin Hope
mahmood
Asked: 2021-01-08 01:55:56 +0800 CST

使用 powershell Invoke-Command 在 C 中安装应用程序:

  • 7

创建虚拟机后,我想在远程机器上安装 anydesk。问题是使用远程 powershell 命令,我无法安装它并获得拒绝访问错误。

Invoke-Command -ComputerName $RemoteMachine -Credential $Cred `
               -ScriptBlock {        
                \\10.1.1.3\share\apps\TOOLS\AnyDesk.exe --install C:\ --start-with-win
               } 

说:

Access is denied
    + CategoryInfo          : OperationStopped: (:) [], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException
    + PSComputerName        : 10.1.1.40

另外,我尝试使用Start-Process以下-Verb选项

 Invoke-Command -ComputerName $RemoteMachine -Credential $Cred 
                -ScriptBlock {        
                 Start-Process -FilePath "\\10.1.1.3\share\apps\TOOLS\AnyDesk.exe" -ArgumentList "--install C:\ --start-with-win" -Verb RunAs
                } 

但是,我得到同样的错误:

Access is denied
    + CategoryInfo          : NotSpecified: (:) [Start-Process], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.St 
   artProcessCommand
    + PSComputerName        : 10.1.1.40

有什么办法可以解决吗?

更新:

在下面的完整代码中,我可以将用户和密码传递给远程机器,并dir在访问\\10.1.1.3. 我还必须强调共享文件夹是公开的。如果我手动登录到我的远程机器(10.1.1.40),我可以在\\10.1.1.3\share\apps\Tools没有任何密码的情况下打开。

$RemoteMachine = "10.1.1.40"
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $RemoteMachine
$Username = 'user'
$Password = ' '
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

Invoke-Command -ComputerName $RemoteMachine -Credential $Cred -ScriptBlock {        
             & {
               dir
               Start-Process -FilePath "\\10.1.1.3\share\apps\TOOLS\AnyDesk.exe" -ArgumentList "--install C:\ --start-with-win" -Verb RunAs
               } }

输出是

PS C:\Windows\system32> C:\Users\user\Desktop\anydesk.ps1


    Directory: C:\Users\user\Documents


Mode                 LastWriteTime         Length Name                     PSComputerName         
----                 -------------         ------ ----                     --------------         
d-----          1/7/2021  12:48 AM                PowerShell               10.1.1.40              
d-----          1/7/2021  12:48 AM                WindowsPowerShell        10.1.1.40              
Access is denied
    + CategoryInfo          : NotSpecified: (:) [Start-Process], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.St 
   artProcessCommand
    + PSComputerName        : 10.1.1.40
powershell
  • 2 个回答
  • 1658 Views
Martin Hope
mahmood
Asked: 2021-01-07 11:52:00 +0800 CST

使用 powershell 运行 diskpart 命令

  • 6

虽然 PowerShell 中有一个Get-Diskcmdlet,但在 Windows 7 上的 PowerShell 2.0 中并不能识别为命令。因此,我使用 diskpart 命令,示例如下:

DiskPart
List disk
Select disk 1
Clean
Create partition primary
Select partition 1
Active
Format FS=NTFS quick
Assign

我想知道是否可以将这些行写入文本文件,然后使用 PowerShell 执行它们。我找不到方便的指南。如何使用 PowerShell 运行 Win32 命令?

powershell diskpart
  • 1 个回答
  • 6358 Views
Martin Hope
mahmood
Asked: 2021-01-07 02:51:54 +0800 CST

在 PowerShell 中连接变量

  • 6

我已经定义了一些 powershell 变量,如下所示

$Host101 = "10.1.1.101"
$Host102 = "10.1.1.102"
$HDD101 = "DataStoreH101"
$HDD102 = "DataStoreH102"
$SSD101 = "DataStoreS101"
$SSD102 = "DataStoreS102"

My-Command -H $Host101 -Disk $HDD101 -Disk $SSD101

现在,我想定义$Node = "101"然后使用以下命令

My-Command -H $Host$Node -Disk $HDD$Node -Disk $SSD$Node

当我测试Write-Host "$HDD$Node"时,它不打印DataStoreH101。我怎么能在powershell中写这样的东西?

powershell
  • 1 个回答
  • 254 Views
Martin Hope
mahmood
Asked: 2020-09-20 07:55:15 +0800 CST

源命令的位置

  • 6

命令的位置在哪里source?当我进入时,which source我什么也得不到。

$ which source
$

有什么注意事项吗?

bash ubuntu
  • 1 个回答
  • 111 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve