如何说服 powershell(通过任务计划程序运行)找到我的网络驱动器?

发布于 2024-09-30 08:17:35 字数 1767 浏览 0 评论 0原文

我在 Windows 7 上有一个简单的 powershell 脚本,但无法正常工作。 (这在 XP 上不是问题)

get-psdrive

当我直接运行它时,我得到

  Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
A                                      FileSystem    A:\
Alias                                  Alias
C                  12.30         11.60 FileSystem    C:\
cert                                   Certificate   \
D                                      FileSystem    D:\
Env                                    Environment
Function                               Function
HKCU                                   Registry      HKEY_CURRENT_USER
HKLM                                   Registry      HKEY_LOCAL_MACHINE
**Q                1486.63        289.41 FileSystem    Q:\**
Variable                               Variable
WSMan                                  WSMan

当我通过任务计划程序运行它时,我得到

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
A                                      FileSystem    A:\
Alias                                  Alias
C                  12.30         11.60 FileSystem    C:\
cert                                   Certificate   \
D                                      FileSystem    D:\
Env                                    Environment
Function                               Function
HKCU                                   Registry      HKEY_CURRENT_USER
HKLM                                   Registry      HKEY_LOCAL_MACHINE
Variable                               Variable
WSMan                                  WSMan

注意我丢失了我的 Q: 驱动器。如果有什么办法可以解决这个问题,我就可以在那里复制文件......

I have a simple powershell script on windows 7 that doesn't work properly. (this is not an issue on XP)

get-psdrive

When I run it directly, I get

  Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
A                                      FileSystem    A:\
Alias                                  Alias
C                  12.30         11.60 FileSystem    C:\
cert                                   Certificate   \
D                                      FileSystem    D:\
Env                                    Environment
Function                               Function
HKCU                                   Registry      HKEY_CURRENT_USER
HKLM                                   Registry      HKEY_LOCAL_MACHINE
**Q                1486.63        289.41 FileSystem    Q:\**
Variable                               Variable
WSMan                                  WSMan

When I run this through task scheduler, I get

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
A                                      FileSystem    A:\
Alias                                  Alias
C                  12.30         11.60 FileSystem    C:\
cert                                   Certificate   \
D                                      FileSystem    D:\
Env                                    Environment
Function                               Function
HKCU                                   Registry      HKEY_CURRENT_USER
HKLM                                   Registry      HKEY_LOCAL_MACHINE
Variable                               Variable
WSMan                                  WSMan

Note that I'm missing my Q: drive. If there's any way to get this resolved, I'll be able to copy files there....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阪姬 2024-10-07 08:17:35

网络驱动器以及实际上所有驱动器号都被“映射”到给定登录会话的卷。当您创建要运行的计划任务时,它会创建一个新的登录会话(即使您当前已登录)并在该上下文中运行计划任务。因此,虽然您可能已登录并映射了 Q 驱动器 - 运行任务的第二个会话具有完全不同的环境,但 Windows 足以自动为所有会话映射 C:(和其他物理驱动器)。

使用 PowerShell 时,您不需要将映射映射到驱动器,除非是为了方便。与 cmd.exe 的前身不同,PowerShell 非常乐意将当前目录更改为 UNC 样式路径:

cd \\server\share\directory

是否可以在根本不映射驱动器的情况下完成您需要的操作?您提到了复制文件 - 如果任务使用您的凭据运行,并假设您拥有 Q: 驱动器的权限(假设 \server\share),那么您的脚本应该能够执行以下操作

copy c:\logs\*.log \\server\share\logs

:需要映射驱动器。

这是我的测试的完整命令信息。如果您的环境不同,请注意如何。仅当我登录、具有最高权限并针对 Windows 7/Server 2008 R2 配置时,该任务才配置为以我的域帐户运行。

该操作是启动一个程序:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

参数

-command copy c:\logs\*.log \\server\share\logs

Network drives, and really all drive letters for that matter, are "mapped" to volumes for a given logon session. When you are creating a scheduled task to run it creates a new login session (even if you are currently logged in) and runs the scheduled task in that context. Thus, while you may be logged in and have a Q drive mapped - the second session that is running the task has a completely different environment, Windows is just nice enough to automatically map the C: (and other physical drives) for all sessions.

You shouldn't need to map a map a drive when using PowerShell, other than for perhaps convenience. Unlike the cmd.exe predecessor, PowerShell is perfectly happy to change the current directory to a UNC style path:

cd \\server\share\directory

Is it possible to accomplish what you need without mapping a drive at all? You have mentioned copying files - if the task is running with your credentials, and assuming you have permissions to the Q: drive (lets say \server\share), then your script should be able to do something like:

copy c:\logs\*.log \\server\share\logs

And work just fine without needing to map a drive.

Here is the complete command info for my test that worked. If your environment is different please note how. The task is configured to run as my domain account, only when I am logged in, highest privileges and configured for Windows 7/Server 2008 R2.

The action is to Start a program:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Arguments

-command copy c:\logs\*.log \\server\share\logs
淡莣 2024-10-07 08:17:35

也许在脚本中运行 get-psdrive 之前,首先执行以下操作:

$net = new-object -comobject Wscript.Network
$net.mapnetworkdrive("Q:","\\path\to\share",0,"domain\user","password")

在完成工作后(复制文件..):

$net.removenetworkdrive("Q:")

Maybe before running get-psdrive in script, firstly do something like this:

$net = new-object -comobject Wscript.Network
$net.mapnetworkdrive("Q:","\\path\to\share",0,"domain\user","password")

and after doing your job (copying files..):

$net.removenetworkdrive("Q:")
习ぎ惯性依靠 2024-10-07 08:17:35

如果您不想在脚本中使用密码,可以使用一个 hack,我更愿意避免这种情况:

  • 以足够的权限打开您的文件夹(例如域用户)
  • 以管理员身份打开 powershell 并创建一个符号链接UNC 到本地路径

    New-Item -ItemType SymbolicLink -Path "C:\LocalTemp\" -Value "\unc"

  • 您现在可以直接在 powershell 脚本中使用 UNC 路径,它将使用计划任务中提供的凭据打开它。

计划任务中的凭据可能存在一些问题,但我认为这仍然比脚本中的明文或伪模糊密码更好。

There is a hack if you don't want to have a password in the script, which I prefer to avoid :

  • Open your folder with sufficient privileges (domain user for example)
  • Open a powershell as Administrator and an make a symlink from UNC to local path

    New-Item -ItemType SymbolicLink -Path "C:\LocalTemp\" -Value "\unc"

  • You can now use the UNC path in your powershell script directly, it will open it with the credential provided in the scheduled task.

There is probably some issues with credentials in scheduled tasks, however this is still better in my opinion than password in clear or pseudo obfuscated in scripts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文