简体   繁体   English

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

[英]How can I convince powershell (run through task scheduler) to find my network drive?

I have a simple powershell script on windows 7 that doesn't work properly.我在 Windows 7 上有一个无法正常工作的简单 powershell 脚本。 (this is not an issue on XP) (这不是 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.请注意,我缺少 Q: 驱动器。 If there's any way to get this resolved, I'll be able to copy files there....如果有任何方法可以解决此问题,我将能够将文件复制到那里....

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.因此,虽然您可能已登录并映射了 Q 驱动器 - 运行该任务的第二个会话具有完全不同的环境,但 Windows 足以为所有会话自动映射 C:(和其他物理驱动器)。

You shouldn't need to map a map a drive when using PowerShell, other than for perhaps convenience.使用 PowerShell 时,您不需要将映射映射到驱动器,这可能只是为了方便。 Unlike the cmd.exe predecessor, PowerShell is perfectly happy to change the current directory to a UNC style path:与 cmd.exe 的前身不同,PowerShell 非常乐意将当前目录更改为 UNC 样式路径:

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:您提到了复制文件 - 如果任务使用您的凭据运行,并假设您拥有 Q: 驱动器的权限(比方说 \\server\\share),那么您的脚本应该能够执行以下操作:

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.该任务配置为作为我的域帐户运行,仅当我登录时,具有最高权限并为 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

Maybe before running get-psdrive in script, firstly do something like this:也许在脚本中运行 get-psdrive 之前,首先做这样的事情:

$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:")

There is a hack if you don't want to have a password in the script, which I prefer to avoid :如果您不想在脚本中使用密码,则有一个 hack,我更愿意避免:

  • 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以管理员身份打开 powershell 并创建从 UNC 到本地路径的符号链接

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

  • You can now use the UNC path in your powershell script directly, it will open it with the credential provided in the scheduled task.您现在可以直接在 powershell 脚本中使用 UNC 路径,它将使用计划任务中提供的凭据打开它。

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.计划任务中的凭据可能存在一些问题,但是在我看来,这仍然比脚本中明文或伪混淆的密码要好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM