我正在使用powershell将数据传输到我的服务器。
我在这里有一个原始问题:Powershell downloadFile with special characters关于试图逃避批量内部的字符串,我没有运气。所以我在这里创建了短的PowerShell脚本:
$model = $args[0]
$os = $args[1]
$gpu = $args[2]
$hdd = $args[3]
$ram = $args[4]
$processor = $args[5]
$realmodel = $args[6]
$realupc = $args[7]
$productnumber = $args[8]
$url = [uri]::EscapeUriString("http://example.com/otherdb/modelfinder/form.php?model=$model&os=$os&graphics=$gpu&hdd=$hdd&ram=$ram&processor=$processor&actualmodel=$realmodel&Implement=0&UPC=$realupc&otherIdentifier=$productnumber")
(New-Object Net.WebClient).DownloadFile($url, 'save.txt')
成功转义字符串并将其发送到我的服务器进行处理。
问题是,在我的网址末尾,有时会出现一个符号#
- 我认为powershell可能会被视为评论,或者根本就没有被编码。在发送URL后检查我的数据库时,#
及其后的所有内容都将从单元格中删除。
如何将要发送的字符串编码为完全保存在数据库中的方式?
答案 0 :(得分:1)
您应该将您的参数作为数据字符串单独转义。当您使用[uri]::EscapeUriString("#")
时,您会看到#
不会被转义。
PS > [uri]::EscapeUriString("#");
#
PS > [uri]::EscapeDataString("#")
%23
因此,作为一个缩写示例,您可以像这样构建字符串:
$sb = [System.Text.StringBuilder]::new();
$model = "# model with hash sign"
$os = "some operating system with exclamation mark!!";
$sb.Append("http://example.com/otherdb/modelfinder/form.php?model=");
$sb.Append([uri]::EscapeDataString($model).ToString());
$sb.Append("&os=");
$sb.Append([uri]::EscapeDataString($os).ToString());
$sb.ToString();
http://example.com/otherdb/modelfinder/form.php?model=%23%20model%20with%20hash%20sign&os=some%20operating%20system%20with%20exclamation%20mark!!