Determine host name via IP address - windows

I need the hostname from my default gateway. It is a Vyos gateway. I can resolve the IP-address by the hostname but not vice verca.
I'm able to get the IP-address from the gateway:
Get-NetRoute -DestinationPrefix "0.0.0.0/0" | Select-Object -ExpandProperty "NextHop"
172.20.1.29
But when I try Resolve-DnsName with that IP, I recieve an error
Resolve-DnsName 172.20.1.29
Resolve-DnsName : 29.1.20.172.in-addr.arpa : DNS name does not exist
At line:1 char:1
+ Resolve-DnsName 172.20.1.29
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (29.1.20.172.in-addr.arpa:String) [Resolve-DnsName], Win32Exception
+ FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName

Related

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')"

DNS Search suffix is being ignored

I'm on a Windows 10 laptop and am receiving the following behaviour:
When I check the DNS Search Suffix List:
PS C:\> Get-DnsClientGlobalSetting
UseSuffixSearchList : True
SuffixSearchList : {domain1.co.uk, domain2.com, domain3.box}
UseDevolution : True
DevolutionLevel : 0
When I resolve the hostname without the suffix:
PS C:\> Resolve-DnsName -Name hostname1
Resolve-DnsName : hostname1 : DNS name does not exist
At line:1 char:1
+ Resolve-DnsName -Name hostname1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (hostname1:String) [Resolve-DnsName], Win32Exception
+ FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName
When I resolve the hostname with the suffix manually specified:
PS C:\> Resolve-DnsName -Name hostname1.domain2.com
Name Type TTL Section IPAddress
---- ---- --- ------- ---------
hostname1.domain2.com A 300 Answer 10.10.10.234
hostname1.domain2.com A 300 Answer 10.10.11.197
Why isn't the suffix "domain2.com" being used to resolve "hostname1" to the IP addresses, as is possible when the suffix is added manually? This should be hapenning automatically, shouldn't it?
I've tried running
ipconfig /flushdns
And I've tried restarting, but it still won't take. What's happening here?
Note: The domain2.com suffix is added automatically when I connect to an Open VPN endpoint, and is specified in the OpenVPN configuration file as:
dhcp-option DOMAIN domain2.com
I've seen posts saying that it's to do with the binding order, and that you can solve this (DNS leak) by increasing the metric for the VPC NIC, so I wrote a script to increase the metric of that card to be the highest out of all cards:
$HighestMetric = ((Get-NetIPInterface).InterfaceMetric | Sort-Object -Descending)[0]
$VpcAdapter = Get-NetAdapter | Where-Object -Property InterfaceDescription -Match "TAP"
$currentMetric = (Get-NetIPInterface -InterfaceAlias $VpcAdapter.InterfaceAlias -AddressFamily IPv4).InterfaceMetric
Set-NetIPInterface -InterfaceAlias $VpcAdapter.InterfaceAlias -InterfaceMetric ($HighestMetric +1)
But that still hasn't solved the issue

powershell parameter PolicyStore and Confirm

I am removing some IP addresses by using:
Remove-NetIPAddress -InterfaceIndex $my.InterfaceIndex.
Which works OK. But when I add the parameter -Confirm
Remove-NetIPAddress -InterfaceIndex $my.InterfaceIndex -Confirm $false.
It fails as follows:
Remove-NetIPAddress : No matching MSFT_NetIPAddress objects found by CIM query for instances of the ROOT/StandardCimv2/MSFT_NetIPAddress
class on the CIM server: SELECT * FROM MSFT_NetIPAddress WHERE ((IPAddress LIKE 'False')) AND ((InterfaceIndex = 15)). Verify query
parameters and retry.
At line:9 char:1
+ Remove-NetIPAddress -InterfaceIndex $my.InterfaceIndex -Confirm $false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (MSFT_NetIPAddress:String) [Remove-NetIPAddress], CimJobException
+ FullyQualifiedErrorId : CmdletizationQuery_NotFound,Remove-NetIPAddress
How should I use the -Confirm, and -PolicyStore parameters?
Because -Confirm is a Switch parameter you pass false to it with a colon:
Remove-NetIPAddress -InterfaceIndex $my.InterfaceIndex -Confirm:$false
You pass true to it by just declaring it on its own.

Delete elasticsearch template in Windows

I am trying to delete elasticsearch template in Windows, however, it keeps giving me error result like this.
PS C:\Users\Administrator> curl -XGET localhost:9200/_template/
Invoke-WebRequest : A parameter cannot be found that matches parameter name 'XGET'.
At line:1 char:6
+ curl -XGET localhost:9200/_template/
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
PS C:\Users\Administrator\Downloads\curator> curl GET localhost:9200/_template/
Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'localhost:9200/_template/'.
At line:1 char:1
+ curl GET localhost:9200/_template/
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Is there anyway to resolve the issues?
curl is an alias for the Invoke-WebRequest cmdlet:
get-command curl | select Name
Name
----
curl
-XGET is equivalent to -Method Get which is the default. So this should work:
Invoke-WebRequest 'http://localhost:9200/_template/'
Note: You probably have to adopt the scheme.

Azure New-AzureManagedCache Error: Value cannot be null

I'm using Azure PowerShell for Create A Cache and I used New-AzureManagedCache -Name test -Location "East Asia" -Sku Standard -Memory 1GB to invoke. The shell has rendered the error Value cannot be null.
PS C:\> New-AzureManagedCache -Name test -Location "East Asia" -Sku Standard -Memory 1GB
New-AzureManagedCache : Value cannot be null.
Parameter name: subscriptionId
At line:1 char:1
+ New-AzureManagedCache -Name test -Location "East Asia" -Sku Standard -Memory 1GB
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureManagedCache], ArgumentNullException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ManagedCache.NewAzureManagedCache
Then I tried using New-AzureManagedCache to find that the error happened in the variable of -Location.
PS C:\> New-AzureManagedCache
cmdlet New-AzureManagedCache at command pipeline position 1
Supply values for the following parameters:
Name: test
Location: "East Asia"
New-AzureManagedCache : Value cannot be null.
Parameter name: subscriptionId
At line:1 char:1
+ New-AzureManagedCache
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureManagedCache], ArgumentNullException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ManagedCache.NewAzureManagedCache
And what kind of value I can input.
Thanks!

Resources