作为特定用户的Powershell脚本
我作为指定用户调用Powershell脚本时遇到问题,希望有人能提供有关如何进行故障排除的信息 我有一个运行脚本,可以将文件移动到名为“Move_files.ps1”的网络位置。当从PS控制台手动运行时,这不会出现问题(我收集到它使用的是我登录时使用的凭据) 在生产中,将使用Oracle APEX(web)前端和windows命令行调用Move_Files.ps1。因此,powershell脚本作为“系统”运行,而用户是“匿名”的,不是有效凭据 到目前为止,我拥有一个名为“RUN.ps1”的powershell脚本,该脚本应该使用特定凭据(具有必要的写入权限)调用“Move_files.ps1”脚本。下列案文是从下列国家通过的 从PS控制台成功调用上述代码(RUN.ps1)并运行Move_files.ps1,但从APEX执行的CMD命令运行RUN.ps1时,无法启动Move_files.ps1 我记录所有打开的PS1文件,以便在文件打开时查看。Start transcript未提供任何可用的输出。如有任何进一步的故障排除步骤或输入,我们将不胜感激 谢谢你花时间帮我解决这个问题作为特定用户的Powershell脚本,powershell,command-line,oracle-apex,credentials,Powershell,Command Line,Oracle Apex,Credentials,我作为指定用户调用Powershell脚本时遇到问题,希望有人能提供有关如何进行故障排除的信息 我有一个运行脚本,可以将文件移动到名为“Move_files.ps1”的网络位置。当从PS控制台手动运行时,这不会出现问题(我收集到它使用的是我登录时使用的凭据) 在生产中,将使用Oracle APEX(web)前端和windows命令行调用Move_Files.ps1。因此,powershell脚本作为“系统”运行,而用户是“匿名”的,不是有效凭据 到目前为止,我拥有一个名为“RUN.ps1”的po
更新1。当PS1脚本从命令提示符启动时,转录本Start没有输出任何结果集。日志文件实际上必须从APEX和命令提示符生成(输出下面的详细信息): 为重复这些步骤的任何人提供的快速操作方法:
调用Powershell脚本的APEX代码
BEGIN SYS.DBMS_SCHEDULER.CREATE_JOB(
job_name => 'PS_scripts' ,
job_type => 'EXECUTABLE',
job_action => 'C:\WINDOWS\system32\cmd.exe',
job_class => 'DEFAULT_JOB_CLASS',
comments => 'Job to test call out to batch script on Windows',
auto_drop => FALSE,
number_of_arguments => 2,
enabled => FALSE);
--SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE( --this function is commented out
--job_name => 'PS_scripts' , --reason is /q turns echo off
--argument_position => 1, --& we need eccho to get the log!
--argument_value => '/q');
SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
job_name => 'PS_scripts' ,
argument_position => 1, argument_value => '/c');
SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
job_name => 'PS_scripts' ,
argument_position => 2,
argument_value => 'powershell "& C:\PowerShell\RUN.ps1" > "C:\Powershell\log.txt"'); --pipe the output here
SYS.DBMS_SCHEDULER.ENABLE( 'PS_scripts' );
END;
我遇到了一个解决这个问题的函数 Powershell后台智能传输服务(BITS)有一个
-Credential
字段,因此可以使用凭据从系统
以外的帐户传输文件
我找到了解决办法
我的Move_Files.ps1代码现在如下所示:
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Function Get-PSCredential($User,$Password)
{
$SecPass = convertto-securestring -asplaintext -string $Password -force
$Creds = new-object System.Management.Automation.PSCredential -argumentlist $User,$SecPass
Return $Creds
}
Import-Module BitsTransfer
$Destination = "\\domain.lan\shared\New_Location"
$credential = Get-PSCredential -User username -Password password
Get-ChildItem -Path "$ScriptPath\Old_Location\*.csv" | Foreach-Object {Start-BitsTransfer -source $_.fullname -destination $Destination -credential $credential}
现在可以从RUN.ps1轻松调用Move_Files.ps1:
start-process Powershell.exe -noprofile -executionpolicy Bypass -file C:\Move_files.ps1
通过映射网络驱动器和预缓存凭据,缩短了解决方案 发现 我的工作代码(Move_files.ps1)
你能把成绩单寄出去吗?同时将erroraction设置为stop以使非终止错误可见谢谢Paul,我现在不在办公桌上,但我一回来就会这么做。谢谢你的insight@Paul,我花了我更长的时间才让日志工作,但我想我已经走上了正轨。我在上面记录了我的步骤。没有真正的进展,但至少我有日志!我在谷歌上搜索了一下,似乎你不能用系统帐户运行启动进程,在另一个线程中,有人建议你尝试psexec,也许这会管用。你可以尝试使用脚本启动一个保存了凭据的计划任务,但我不确定这是否管用。这是使用服务帐户而不是依赖
系统
或网络服务
的主要原因之一。
BEGIN SYS.DBMS_SCHEDULER.CREATE_JOB(
job_name => 'PS_scripts' ,
job_type => 'EXECUTABLE',
job_action => 'C:\WINDOWS\system32\cmd.exe',
job_class => 'DEFAULT_JOB_CLASS',
comments => 'Job to test call out to batch script on Windows',
auto_drop => FALSE,
number_of_arguments => 2,
enabled => FALSE);
--SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE( --this function is commented out
--job_name => 'PS_scripts' , --reason is /q turns echo off
--argument_position => 1, --& we need eccho to get the log!
--argument_value => '/q');
SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
job_name => 'PS_scripts' ,
argument_position => 1, argument_value => '/c');
SYS.DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(
job_name => 'PS_scripts' ,
argument_position => 2,
argument_value => 'powershell "& C:\PowerShell\RUN.ps1" > "C:\Powershell\log.txt"'); --pipe the output here
SYS.DBMS_SCHEDULER.ENABLE( 'PS_scripts' );
END;
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Function Get-PSCredential($User,$Password)
{
$SecPass = convertto-securestring -asplaintext -string $Password -force
$Creds = new-object System.Management.Automation.PSCredential -argumentlist $User,$SecPass
Return $Creds
}
Import-Module BitsTransfer
$Destination = "\\domain.lan\shared\New_Location"
$credential = Get-PSCredential -User username -Password password
Get-ChildItem -Path "$ScriptPath\Old_Location\*.csv" | Foreach-Object {Start-BitsTransfer -source $_.fullname -destination $Destination -credential $credential}
start-process Powershell.exe -noprofile -executionpolicy Bypass -file C:\Move_files.ps1
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$Destination = ""\\domain.lan\shared\New_Location"
$username = "username"
$password = "password"
net use $Destination $password /USER:$username
Get-ChildItem -Path "$ScriptPath\Old_Location\*.csv" | Foreach-Object { copy-item -Path $_.fullname -Destination $Destination }
#delete the share
net use $Destination /delete