Get file version from .exe URL-Source in PowerShell - windows

I know how to get productversion of a file on my windows PC with powershell
(get-item -Path
'(get-item -Path 'C:\test.exe').VersionInfo.ProductVersion
but is it also possible to get the productversion from a exe file with it's URL?
(get-item -Path 'example.com/test.exe').VersionInfo.ProductVersion

No, it is not possible to get the product version from a URL.
When we use a URL it is not the direct path to access the storage server. Therefore we can not determine the version. By C path we can not access the URL Path.
I tried the shell command for Teamviewer setup URL Teamviewer version 9.X. It is not possible to get it through the URL.
PS C:\Users\israr ahmad> (get-item -Path 'download.teamviewer.com/full').VersionInfo.ProductVersion
get-item : Cannot find path 'C:\Users\israr ahmad\download.teamviewer.com\full' because it does not exist.
At line:1 char:2
+ (get-item -Path 'download.teamviewer.com/full').VersionInfo.ProductVe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\israr ...viewer.com\full:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
PS C:\Users\israr ahmad> ^C
PS C:\Users\israr ahmad> https://download.teamviewer.com/download/version_9x/TeamViewer_Setup.exe
+ (get-item -Path 'https://www.teamviewer.com/download/version_9x/Team ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (https:String) [Get-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetItemCommand
PS C:\Users\israr ahmad> (get-item -Path '../www.teamviewer.com/download/version_9x/TeamViewer_Setup.exe').VersionInfo.ProductVersion
get-item : Cannot find path 'C:\Users\www.teamviewer.com\download\version_9x\TeamViewer_Setup.exe' because it does not exist.

Related

Active Directory Member Of Custom Pull Script

I put together this script over 5 years ago now and since have forgotten what I did or how I did it...
For some context, the below script which runs from a .bat file was used to enter a user's display name and then using dsquery and dsget, along with Get-Content from PowerShell commands. About 6 months ago this script stopped working and just crashes and fails with a brief error that it getting stuck on this line to my understanding:
dsquery user forestroot -name "%lookforuser%">"%userprofile%\Desktop\usertemp.txt"
The %lookforuser% variable is set earlier in the script, see below for full source code:
#echo off
title Get AD Groups For User
set /p lookforuser=Enter Username (Surname, Firstname):
set lookforuser2=%lookforuser: =%
echo %lookforuser%
echo %lookforuser2%
dsquery user forestroot -name "%lookforuser%">"%userprofile%\Desktop\usertemp.txt"
set /p usercn=<"%userprofile%\Desktop\usertemp.txt"
dsget user %usercn% -memberof>"%userprofile%\Desktop\usertemp.txt"
Powershell -Command "(Get-Content %userprofile%\Desktop\usertemp.txt) -replace '\"CN=','' ^| Out-File -encoding ASCII %userprofile%\Desktop\usertemp.txt"
Powershell -Command "(Get-Content %userprofile%\Desktop\usertemp.txt) -replace ',OU=','*' | Out-File -encoding ASCII %userprofile%\Desktop\usertemp.txt"
for /f "delims=*, tokens=1" %%A IN (%userprofile%\Desktop\usertemp.txt) do echo %%A>>"%userprofile%\Desktop\%lookforuser2%.txt"
del "%userprofile%\Desktop\usertemp.txt"
Long story short, can anyone help me figure out what's gone on, has my parent company changed something to force this not to work, or has MS changed something that no longer allows this script to run, really stumped and not sure.
Any help would be greatly appreciated, even if you have something similar that works in Powershell
Please see error in command prompt below:
Get-Content : Cannot find path 'C:\Users\mcguirkd\Desktop\usertemp.txt' because it does not exist.
At line:1 char:2
+ (Get-Content C:\Users\mcguirkd\Desktop\usertemp.txt) -replace '"CN=', ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\mcguirkd\Desktop\usertemp.txt:String) [Get-Content], ItemNotFo
undException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Out-File : Could not find a part of the path 'C:\Users\mcguirkd\Desktop\usertemp.txt'.
At line:1 char:75
+ ... '"CN=','' | Out-File -encoding ASCII C:\Users\mcguirkd\Desktop\userte ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], DirectoryNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
Get-Content : Cannot find path 'C:\Users\mcguirkd\Desktop\usertemp.txt' because it does not exist.
At line:1 char:2
+ (Get-Content C:\Users\mcguirkd\Desktop\usertemp.txt) -replace ',OU=', ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\mcguirkd\Desktop\usertemp.txt:String) [Get-Content], ItemNotFo
undException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Out-File : Could not find a part of the path 'C:\Users\mcguirkd\Desktop\usertemp.txt'.
At line:1 char:76
+ ... ,OU=','*' | Out-File -encoding ASCII C:\Users\mcguirkd\Desktop\userte ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], DirectoryNotFoundException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand

How do I download a file using windows command line without having to input a proxy password?

After looking at various stackoverflow questions, I found several ways to download a file from a command line without interaction from the user.
The only one that worked for me also works only on Windows 10 natively :
curl -sko %TEMP%\file.txt "https://some.hostname/file.txt"
But installing an external tool like wget/curl is what I want to avoid.
What didn't work for me because of proxy errors :
Command:
bitsadmin.exe /transfer "dljob" "https://some.hostname/file.txt" %TEMP%\file.txt
Error:
DISPLAY: 'dljob' TYPE: DOWNLOAD STATE: ERROR
PRIORITY: NORMAL FILES: 0 / 1 BYTES: 0 / UNKNOWN
Unable to complete transfer.
ERROR FILE: https://some.hostname/file.txt -> E:\Users\xxx\AppData\Local\Temp\file.txt
ERROR CODE: 0x80190197
ERROR CONTEXT: 0x00000005
Command:
powershell -Command "(New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt', '%TEMP%\file.txt')"
Error:
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (407) Proxy Authentication Required."
At line:1 char:1
+ (New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Command:
powershell -Command "Invoke-WebRequest 'https://some.hostname/file.txt' -OutFile %TEMP%\file.txt
Error:
Invoke-WebRequest :
Authentication required
You must be authenticated to access this URL.
...
Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.17763.1007
At line:1 char:1
+ Invoke-WebRequest 'https://some.hostname/file.txt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
This didn't work either :
powershell -Command "$client.Credentials = Get-Credential; $browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials; (New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt', 'file.txt')"
Error :
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Get-Credential : Cannot process command because of one or more missing mandatory parameters: Credential.
At line:1 char:23
+ $client.Credentials = Get-Credential; $browser.Proxy.Credentials =[Sy ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Credential], ParameterBindingException
+ FullyQualifiedErrorId : MissingMandatoryParameter,Microsoft.PowerShell.Commands.GetCredentialCommand
The property 'Credentials' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:39
+ ... Credential; $browser.Proxy.Credentials =[System.Net.CredentialCache]: ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (407) Proxy
Authentication Required."
At line:1 char:124
+ ... redentials; (New-Object Net.WebClient).DownloadFile('https://some.hostname ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Refer to this question Access web using Powershell and Proxy
You can try something like that in Powershell and suppose that you have already created a folder named as C:\Test:
$url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
$file = "C:\Test\" + $url.Split("/")[-1]
$wb = New-Object System.Net.WebClient
$wb.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$wb.DownloadFile($url,$file)
EDIT : 14/08/2020 #17:08
I tried this on Windows Powershell ISE and it works 5/5 :
cls
$start_time = Get-Date
$url = "https://cdn2.unrealengine.com/Fortnite%2FBoogieDown_GIF-1f2be97208316867da7d3cf5217c2486da3c2fe6.gif"
$Folder = "$Env:Temp\DownloadFolder"
# We create a SubFolder Named "DownloadFolder" in the temporary file %Temp% if it doesn't exists yet !
If ((Test-Path -Path $Folder) -eq 0) { New-Item -Path $Folder -ItemType Directory | Out-Null }
# We can get the name of the file to be downloaded from the variable $url
# $url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
# In our case the FileName will be = "googlelogo_color_272x92dp.png" or
# Fortnite%2FBoogieDown_GIF-1f2be97208316867da7d3cf5217c2486da3c2fe6.gif
$file = $Folder+ "\" + $url.Split("/")[-1]
Try
{
$wb = New-Object System.Net.WebClient
$wb.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$wb.DownloadFile($url,$file)
# better use Invoke-Item $Folder instead of ii
Invoke-Item $Folder
Write-Output "Running Script Time taken is : $((Get-Date).Subtract($start_time).Milliseconds) millisecond(s)"
}
Catch
{
Write-Host "Error from $url" `n"Message: [$($_.Exception.Message)"] -ForegroundColor Red -BackgroundColor DarkBlue
}
This worked for me :
powershell -Command "[System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy(); [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials; (New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt', 'file.txt')"

How to change date modified of C:\$RECYCLE.BIN?

Since you can only view the C:\$RECYCLE.BIN Folder when you uncheck (Hide protected Operating System Files) you can't change the date modified through the usual way with PowerShell:
$file = Get-Item C:\Path\TO\File.txt
$file.LastWriteTime = (Get-Date)
or
$folder = Get-Item C:\folder1
$folder.LastWriteTime = (Get-Date)
When calling Get-Item C:\$RECYCLE.BIN, I get the following error:
Get-Item : Cannot find path 'C:\.bin' because it does not exist.
At line:1 char:9
+ $file = Get-Item C:\$Recycle.bin
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\.bin:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
How would I go about changing the date modified of it?
As C:\$RECYCLE.BIN contains $, PowerShell tries to evaluate the variable $RECYCLE, which is empty. Thus, the resulting path C:\.bin cannot be found. Quote your path with single quotes (') to prevent PowerShell from evaluating your string and also add the -Force parameter as it is a system directory:
Get-Item -Path 'C:\$RECYCLE.BIN' -Force
You can then access your files and change the LastWriteTime as you already tried in your question:
$fileObject = Get-Item -Path 'C:\$RECYCLE.BIN\S-1-5-21-3323847664-626704455-790384891-1001\$RT8USDF.txt' -Force
$fileObject.LastWriteTime = Get-Date

Copy-Item for copy files from local to remove server using credentials

I am trying to copy some files and folder from my local machine to a remote server:
Copy-Item .\copy_test.txt -destination "\\serverip\c$\backups\"
but I'm getting an error:
Copy-Item : Logon failure: unknown user name or bad password.
At line:1 char:10
+ Copy-Item <<<< .\copy_test.txt -destination "\\serverip\c$\backups\" -verbose
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
I was trying using credentials but this command does not allow -Credential argument. I was searching a lot and in every example the command is pretty easy just executing the Copy-Item $source -destination $destination and I wonder why is so hard in my workstation.
Creating New PSDrive
I tried to create a New-PSDrive but it didn't work.
$creds = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username, $password
New-PSDrive -Name X -PSProvider FileSystem -Root '\\$serverip\c$' -Credential $creds -Persist
Copy-Item '.\copy_test.txt' -Destination 'X:\backups'
Remove-PSDrive -Name X
It is the error message:
PS C:\Users\Administrator\Desktop> .\copyfiles.ps1
New-PSDrive : The network path was not found
At C:\Users\Administrator\Desktop\copyfiles.ps1:11 char:1
+ New-PSDrive -Name X -PSProvider FileSystem -Root '\\$serverip\c$' -Credential $c ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (X:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveC
Copy-Item : Cannot find drive. A drive with the name 'X' does not exist.
At C:\Users\Administrator\Desktop\copyfiles.ps1:12 char:1
+ Copy-Item '.\copy_test.txt' -Destination 'X:\backups'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (X:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Remove-PSDrive : Cannot find drive. A drive with the name 'X' does not exist.
At C:\Users\Administrator\Desktop\copyfiles.ps1:13 char:1
+ Remove-PSDrive -Name X
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (X:String) [Remove-PSDrive], DriveNotFoundExcepti
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand
My servers
My server are windows instances in AWS. I have the right permission because I am able to run other command like Invoke-Command in order to inspect some services into the remote server.
PS> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
If credentials are required for access to a remote share you need to map it to a (PS)drive before you can use it with other cmdlets.
$cred = Get-Credential
New-PSDrive -Name X -PSProvider FileSystem -Root "\\$serverip\c$" -Credential $cred -Persist
Copy-Item '.\copy_test.txt' -Destination 'X:\backups'
Remove-PSDrive -Name X
I found the solution. I was using PowerShell version 4.0 and then upgrade my version to 5.0
In previous version the Copy-Item doesn't allow credentials. Now is possible to copy files through the sessions between servers:
$deploy_dest = "C:\backup"
$username = "$server\Administrator"
$password = Get-Content C:\mypassword.txt | ConvertTo-SecureString -AsPlainText -Force
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$session = New-PSSession -ComputerName $server -Credential $creds
Copy-Item -Path .\copy_test.txt -Destination -ToSession $session
Check this alternative using copy command and net use
net use \\10.164.60.77\c$\Users\ana\Desktop password /user:username
copy "C:\Users\alex\Desktop\test.txt" "\\10.164.60.77\c$\Users\ana\Desktop\test.txt"

Remove-Item doesn't work, Delete does

Does anyone have any idea why Remove-Item would fail while Delete works?
In below script, I get a list of files I'd like to delete.
Using Remove-Item I get following error message:
VERBOSE: Performing the operation "Remove File" on target
"\\UncPath\Folder\test.rtf". Remove-Item : Cannot remove item
\\UncPath\Folder\test.rtf: Access to the path is denied.
but using Delete is deleting those files as we speak.
Script
$files = gci \\UncPath\Folder| ?{ $_.LastWriteTime -le (Get-Date).addDays(-28) }
# This doesn't work
$files | Remove-Item -force -verbose
# But this does
$files | % { $_.Delete() }
powershell may act strange with UNC path, I think it prepends the UNC Path with the current provider you can verify this with :
cd c:
test-path \\127.0.0.1\c$
returns TRUE
cd HKCU:
test-path \\127.0.0.1\c$
returns FALSE
when specifying the fullpath we're telling powershell to use the filesystem provider, that solves the problem. you could also specify the provider like remove-item filesystem::\\uncpath\folder
I can finally repro this and IMO it appears to be a bug. The repro is to have an open share like C$ but to set Deny Modify perms for the user on the file. When I do that, I observe this:
PS> gci '\\Keith-PC\C$\Users\Keith\foo.txt' | ri -for
ri : Cannot remove item \\Keith-PC\C$\Users\Keith\foo.txt: Access to the path is denied.
At line:1 char:43
+ gci '\\Keith-PC\C$\Users\Keith\foo.txt' | ri -for
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (\\Keith-PC\C$\Users\Keith\foo.txt:FileInfo) [Remove-Item], ArgumentExc
eption
+ FullyQualifiedErrorId : RemoveFileSystemItemArgumentError,Microsoft.PowerShell.Commands.RemoveItemCommand
PS> gci '\\Keith-PC\C$\Users\Keith\foo.txt' | %{$_.Delete()} # <== this works!
I also observe that removing the -Force parameter deletes the file without error as well. The deny perms still allow me to delete the file from Windows Explorer so that leads me to believe that the file should delete. So what is up with using the -Force parameter? When I delve into the ErrorRecord I see this:
Message : Access to the path is denied.
ParamName :
Data : {}
InnerException :
TargetSite : Void set_Attributes(System.IO.FileAttributes)
StackTrace : at System.IO.FileSystemInfo.set_Attributes(FileAttributes value)
at Microsoft.PowerShell.Commands.FileSystemProvider.RemoveFileSystemItem(FileSystemInfo
fileSystemInfo, Boolean force)
It seems that the -Force parameter is trying to set (more likely reset) attributes and the permissions on the file don't allow it e.g.:
PS> gci '\\Keith-PC\C$\Users\Keith\foo.txt' | %{$_.Attributes = 'Normal'}
Exception setting "Attributes": "Access to the path is denied."
At line:1 char:45
+ gci '\\Keith-PC\C$\Users\Keith\foo.txt' | %{$_.Attributes = 'Normal'}
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
So it seems to me that PowerShell should first try as if the -Force weren't present and if that fails, then try resetting attributes.

Resources