[Solved by Superfly] How to skip repetitive links?

Discussion in 'Scripting' started by xinso, Jun 14, 2015.

  1. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,697
    13,699
    340
    #21 xinso, Jun 17, 2015
    Last edited: Jun 17, 2015
    (OP)
    Good.

    Here is another problem:

    With one ESD folder in C:\Windows\SoftwareDistribution\Download, it is fast and accurate for the Powershell to get the link.

    On the contrary, it becomes slow and inaccurate
    furthermore when there were more ESD folders.

    If we use
    net stop BITS to remove C:\Windows\SoftwareDistribution\Download everytime before we start to get the link, then it

    revert to fast again.

    To sum it up:

    net stop BITS needs more time (T x 1)

    slow and inaccurate
    needs even more time (T x N)

    So I think it would be better to polish the whole processes for sake of accuracy and efficiency.

    You have helped me a lot, so I will do it myself and feedback to you. May I?
     
  2. Superfly

    Superfly MDL Expert

    Jan 12, 2010
    1,143
    543
    60
    Sure, let me know where I can help.

    I looked into Remove-BitsTransfer - the d/l's are owned by NT Authority and I can't get PS to run under that credentials...will check it out some more.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,697
    13,699
    340
    #23 xinso, Jun 17, 2015
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Feedback

    Yes. It is a bit annoyance.

    There is runassystem.exe and runfromtoken.exe in place. But I do not like third party binaries, I adopt net stop BITS instead.

    There is an easy MS command that can accomplish this purpose. But I forgot where it is now.

    OK. Here are the scripts I use now: Always run 3-Restart.cmd once before starting the link-fetching.

    STEP 1: 1-WUapp.cmd
    Code:
    @echo off
    pushd "%~dp0"
    
    wuapp
    
    STEP 2: Check for updates

    STEP 3: 2-Get-Link.cmd
    Code:
    @echo off
    pushd "%~dp0"
    
    for /f "tokens=7  delims=\" %%a in ('"reg query HKLM\SYSTEM\ControlSet001\Control\MUI\UILanguages" 2^>nul') do (set UI=%%a)
    
    CMD /c PowerShell.exe -ExecutionPolicy Bypass "Get-BitsTransfer -AllUsers | Select -ExpandProperty FileList | Select -ExpandProperty RemoteName | Where-Object {$_ -match '%UI%' -and $_ -match '.esd' } | Add-Content $env:userprofile\desktop\ESD-url.txt"
    
    CMD /c PowerShell.exe -ExecutionPolicy Bypass "Get-Content $env:userprofile\desktop\ESD-url.txt | Select-Object -Unique | Set-Content $env:userprofile\desktop\ESD-url.txt"
    
    net stop wuauserv >nul 2>&1
    STEP 4: 3-Restart.cmd
    Code:
    @echo off
    pushd "%~dp0"
    
    net stop wuauserv
    net stop BITS
    rd /s /q "%ALLUSERSPROFILE%\Application Data\Microsoft\Network\Downloader"
    rd /s /q "C:\Windows\SoftwareDistribution\Download"
    shutdown /r /t 0
     
  4. Superfly

    Superfly MDL Expert

    Jan 12, 2010
    1,143
    543
    60
    #24 Superfly, Jun 17, 2015
    Last edited by a moderator: Apr 20, 2017
    Looking good... :cool:

    I have an idea about not having to restart after clearing bits... by importing the module before Get-BitsTranfer is run - may or may not work.

    It will require having to change the command to run the ps1 though.

    Get-Links.ps1:
    Code:
    ################################################ Elevate #######################################################
    $myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
    $myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
    $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
    
    if (-not $myWindowsPrincipal.IsInRole($adminRole))
    
      {
        Start-Process PowerShell.exe -Verb Runas -WindowStyle Hidden -ArgumentList $myInvocation.MyCommand.Definition
        return
       }
    ################################################################################################################>
    
    [string] $UI = Get-ChildItem -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\MUI\UILanguages' -Name -ErrorAction Inquire
    [string] $path = $env:userprofile +'\Desktop\ESD-url.txt'
    
    Import-Module BitsTransfer
    
    #Get links
    Get-BitsTransfer -AllUsers | 
    Select -ExpandProperty FileList | 
    Select -ExpandProperty RemoteName | 
    Where {$_ -match $UI -and $_ -match '.esd'}| 
    Add-Content $path 
    
    #Remove duplicates from file
    if (Test-Path $path){
    Get-Content $path |
    Select-Object -Unique |
    Set-Content $path 
    }

    BTW: Can you net start BITS after deleting folders...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. xinso

    xinso MDL Guru

    Mar 5, 2009
    12,697
    13,699
    340
    #25 xinso, Jun 17, 2015
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Good. I will test it.

    Feedback:

    1. It is working fine. And the rough Get-Links.cmd I used for Get-Links.ps1

    Code:
    @echo off
    pushd "%~dp0"
    
    set E0=if errorlevel 0
    for /f "tokens=7  delims=\" %%a in ('"reg query HKLM\SYSTEM\ControlSet001\Control\MUI\UILanguages" 2^>nul') do (set UI=%%a)
    
    CMD /c PowerShell.exe -ExecutionPolicy Bypass "%~dp0Get-Links.ps1"
    type "%~dp0ESD-url.txt" | findstr /i %UI% &&%E0% (goto:STOP)
    
    echo.
    echo NOT OK 1st time
    echo.
    
    CMD /c PowerShell.exe -ExecutionPolicy Bypass "%~dp0Get-Links.ps1"
    type "%~dp0ESD-url.txt" | findstr /i %UI% &&%E0% (goto:STOP)
    
    echo.
    echo NOT OK 2nd time
    echo.
    pause
    exit
    
    :STOP
    net stop wuauserv >nul 2>&1
    taskkill /f /im SystemSettings.exe >nul 2>&1
    echo.
    pause
    Result
    Code:
    http://fg.v4.sh.dl.ws.microsoft.com/dl/content/d/updt/2015/05/10130.0.150522-2224.fbl_impressive_clientpro_ret_x64fre_es-es_b1506add36e9a332efd338666826acadea4d3828.esd?P1=1434556875&P2=101&P3=1&P4=emxQdzv34K%2fCg0Ay07l0XFKKUrI%3d
    
    Press any key to continue . . .
    2. Can you net start BITS after deleting folders...

    Yes. It does. But it turns to to abnormal without a restart.

    Windows Update frozen in downloading ESD, and the script...

    Get-Content : Cannot find path 'C:\Users\A\desktop\ESD-url.txt' because it does not exist.
    At line:1 char:1
    + Get-Content $env:userprofile\desktop\ESD-url.txt | Select-Object -Uni ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (C:\Users\A\desktop\ESD-url.txt:String) [Get-Content], ItemNotFoundExcep
    tion
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand


    The system cannot find the file specified.


    NOT OK 1st time


    Get-Content : Cannot find path 'C:\Users\A\desktop\ESD-url.txt' because it does not exist.
    At line:1 char:1
    + Get-Content $env:userprofile\desktop\ESD-url.txt | Select-Object -Uni ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (C:\Users\A\desktop\ESD-url.txt:String) [Get-Content], ItemNotFoundExcep
    tion
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand


    The system cannot find the file specified.


    NOT OK 2nd time


    Waiting for 3 seconds, press CTRL+C to quit ...
     
  6. Superfly

    Superfly MDL Expert

    Jan 12, 2010
    1,143
    543
    60
    Yup, I also noticed errors after net start BITS...as you said it takes a while to initialize...maybe it's safer to do a restart anyway.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. Superfly

    Superfly MDL Expert

    Jan 12, 2010
    1,143
    543
    60
    #27 Superfly, Jun 18, 2015
    Last edited by a moderator: Apr 20, 2017
    Xinso, here's another way to bring back only the current bits transfer - by checking the jobstate...

    Code:
    #Get links
    Get-BitsTransfer -AllUsers |
    Where {$_.JobState -eq "Transferring"} |
    Select -ExpandProperty FileList |
    Select -ExpandProperty RemoteName | 
    Where {$_ -match $UI -and $_ -match '.esd'}| 
    Add-Content $path  -ErrorAction Continue
    - you can check the jobstate after is starts by running Get-BitsTransfer -AllUsers.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...