使用PowerShell从多个源复制到多个目标

时间:2016-04-27 10:49:52

标签: sql-server powershell powershell-v2.0

是否可以使用PowerShell将文件从多个源复制到多个目标?我已经研究了很多,但找不到任何解决方案。这些多个源和目标的信息位于SQL表中。任何人都可以指出我正确的方向或提出如何接近的建议吗?

我使用以下代码填充数据表中的数据。现在我想使用包含源和目标位置的数据表中的数据并运行副本

var invalidEmailIds = groups.Where(group => !group.Key)
                        .SelectMany(group => group);

$dataSource = “.\SQLEXPRESS” $user = “user” $pwd = “1234” $database = “Test” $connectionString = “Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;” $connection = New-Object System.Data.SqlClient.SqlConnection $connection.ConnectionString = $connectionString $connection.Open() $query = “SELECT * FROM Source&destinationinfo” $command = $connection.CreateCommand() $command.CommandText = $query $table = new-object “System.Data.DataTable” $table.Load($result) 中的数据如下:

[Source&destinationinfo]

1 个答案:

答案 0 :(得分:2)

您可以在SQL端执行此操作:

  1. 创建将从表中获取数据并创建脚本的存储过程。
  2. 示例:

    CREATE PROCEDURE dbo.create_a_script_and_write_to_file
    AS
    BEGIN
        SET NOCOUNT ON;
    
        DECLARE @x nvarchar(max),
                @filename nvarchar(max) = 'D:\file.ps1'
    
        --here we generate script and put in @x
        SELECT @x = STUFF((
        SELECT top 5 'copy-item -Path "'+ PathFrom + '" -Destination "' + PathWhere + '" '
        FROM dbo.YourTable
        FOR XML PATH('')
        ),1,0,'')
    
    
        --here we write @x in file
        DECLARE @FSO int
        DECLARE @hr int
        DECLARE @src varchar(255)
        DECLARE @desc varchar(255)
        DECLARE @oFile int
    
    
        -- Create OLE Automation Object
        EXEC @hr = sp_OACreate 'Scripting.FileSystemObject', @FSO OUT
        IF @hr <> 0
        BEGIN
           EXEC sp_OAGetErrorInfo @FSO, @src OUT, @desc OUT 
           SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc
            RETURN
        END 
    
        -- Create the XML file
        EXEC @hr = sp_OAMethod @FSO, 'CreateTextFile', @oFile OUT, @filename, 8 , True
        IF @hr <> 0
        BEGIN
           EXEC sp_OAGetErrorInfo @FSO, @src OUT, @desc OUT 
           SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc
            RETURN
        END
    
        EXEC @hr = sp_OAMethod @oFile, 'Write', NULL, @x
        IF @hr <> 0
        BEGIN
           EXEC sp_OAGetErrorInfo @FSO, @src OUT, @desc OUT 
           SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc
            RETURN
        END
    
        -- Clear used objects
        EXEC @hr = sp_OADestroy @FSO
        IF @hr <> 0
        BEGIN
          EXEC sp_OAGetErrorInfo @FSO, @src OUT, @desc OUT 
          SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc
           RETURN
        END
        EXEC @hr = sp_OADestroy @oFile
        IF @hr <> 0
        BEGIN
            EXEC sp_OAGetErrorInfo @oFile, @src OUT, @desc OUT 
            SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc
             RETURN
        END 
    
    END
    
    1. 创建工作。
    2. 第一步是EXEC dbo.create_a_script_and_write_to_file,下一步将是操作系统类型(CmdExec)powershell.exe -File "D:\file.ps1"

      运行此作业后,表格PathFrom列中的所有文件都将移至PathWhere